TXT文本储存
# 保存知乎上“发现”页面的“热门话题”的问题与答案
import requests
from pyquery import PyQuery as pq
url = 'https://www.zhihu.com/explore'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 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() # 热门文章标题
author = item.find('.author-link-line').text() # 作者名
answer = pq(item.find('.content').html()).text() # 文章主体
file = open('explore.txt', 'a', encoding='utf-8') 打开文件,文件名,写入方式,编码格式
file.write('\n'.join([question, author, answer])) # 写入数据
file.write('\n' + '=' * 50 + '\n') # 分割线
file.close() # 关闭文件