requests + pyquery爬取知乎发现首页

简述

学习了崔神的代码。
然后自己手动敲了下,mark一下

代码

import requests
from pyquery import PyQuery as pq

url = 'https://www.zhihu.com/explore'

headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36'
}
html = requests.get(url, headers=headers).text
doc = pq(html)
items = doc('.explore-tab .feed-item').items()

for item in items:
    question = item.find('h2').text()  # get question
    author = item.find('.author-link-line a').text()  # get athor name
    answer = pq(item.find('.content').html()).text()
    with open('explore.txt', 'a', encoding='utf-8') as f:
        f.write('\n'.join([question, 'Author : '+author, answer]))
        f.write(('=' * 50).join(['\n'] * 2))

你可能感兴趣的:(Python,爬虫)