python 【requests pyquery】

 1 import requests
 2 
 3 from pyquery import PyQuery as pq
 4 
 5 url = "https://www.zhihu.com/explore"
 6 
 7 headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"}
 8 
 9 html = requests.get(url,headers=headers).text
10 
11 doc = pq(html)
12 
13 items = doc(".explore-tab .feed-item").items()
14 
15 for item in items:
16     question = item.find("h2").text()
17     author = item.find(".author-link-line").text()
18     answer = pq(item.find(".content").html()).text()
19     file = open("explore.txt","a",encoding="utf-8")
20     file.write('\n'.join([question,author,answer]))
21     file.write('\n' + '=' *50 +'\n')
22     file.close()
pyquery

 

posted @ 2018-07-20 20:13  Justice-V  阅读(112)  评论(0)    收藏  举报