Python3爬虫(八) 数据存储之TXT、JSON、CSV
http://www.cnblogs.com/Infi-chu/ TXT文本存储 TXT文本存储,方便,简单,几乎适用于任何平台。但是不利于检索。 1.举例: 使用requests获得网页源代码,然后使用pyquery解析库解析 import requests from pyquery import PyQuery as pq url = 'https://www.zhihu.com/explore' header = { 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)' } html = requests.get(url,headers=header).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('test.txt','a',encoding='uft