3.1 保存爬虫数据至Txt文本
1.保存数据至TXT文本中
with open('save_data_to_txt.txt', 'a', encoding='utf-8') as f:
f.write('\n'.join([question, author, answer]))import requests
from pyquery import PyQuery as pq
base_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.99 Safari/537.36',
}
html = requests.get(base_url,headers=headers).text
doc = pq(html)
items = doc('.explore-tab .feed-item').items()
for item in items:
question = item.find('h2').text()
author = item.find('.author-link-line').text()
answer = pq(item.find('.content').html()).text()
with open('zhihu-explore.txt', 'a', encoding='utf-8') as f:
f.write('\n'.join([question, author, answer]))
f.write('\n' + '=' * 50 + '\n')Last updated