4.数据存储
文本存储
TXT 文件存储
实例
import requests
from pyquery import PyQuery as pq
url = "https://www.zhihu.com/explore"
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
}
response = requests.get(url,headers=headers)
content = response.text
# pq解析
doc = pq(content)
items = doc('.explore-tab .feed-item').items()
for item in items:
question = item('.question_link').text()
author = item('.avatar-link').text()
answer = pq(item.find('.content').html()).text()
with open('explore.txt','a',encoding='utf-8') as f:
f.write('\n'.join([question,author,answer]))
f.write('\n'+'='*50+'\n')打开方式
写法
JSON文件存储
说明
取Json
输出json
CSV文件存储
说明
写入
读取
数据库
关系数据库
非关系数据库
ORM模型
最后更新于
这有帮助吗?
