爬虫基础(二)

2.1 Session

2.2 Cookie

2.3 爬取丁香园论坛的回复内容

'''
爬取丁香园论坛的回复内容
'''

import requests
from pyquery import PyQuery as pq

BASE_URL = 'http://www.dxy.cn/bbs/thread/626626#626626'


def get_replies():
    html = requests.get(BASE_URL).text
    print('html', html)
    doc = pq(html)
    tds = doc('td.postbody').items()
    replies = [td.text().strip() for td in tds if td.text().strip()]
    print('一共有' + str(len(replies)) + '条回复')
    return replies


if __name__ == '__main__':
    replies = get_replies()
    for reply in replies:
        print(reply)

猜你喜欢

转载自blog.csdn.net/the_harder_to_love/article/details/89163927