python将客户的数据一直保存的方法:1、使用【with open()】新建对象写入数据;2、使用pandas包保存,代码为【import pandas as pd #导入pandas】。
【相关学习推荐:python教程】
python将客户的数据一直保存的方法:
一、open函数保存
使用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))#在写代码的时候可以将读取的内容打印一下
#保存数据open函数
with open('D:/PythonWorkSpace/TestData/pinglun.txt