Python 保存数据的方法:
使用with open()新建对象
写入数据(这里使用的是爬取豆瓣读书中一本书的豆瓣短评作为例子)
import requests
from lxml import etree
#发送Request请求
url = 'https://book.douban.com/subject/1054917/comments/'
head = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36'}
#解析HTML
r = requests.get(url, headers=head)
s = etree.HTML(r.text)
comments = s.xpath('//div[@class="comment"]/p/text()')
#print(str(comments))