先上代码
`#-- coding=utf-8 --
import requests
from lxml import etree
url=‘https://book.douban.com/subject/27180479/comments/’
r=requests.get(url)
s=etree.HTML(r.text)
kk=s.xpath(’//*[@id=“comments”]/ul/li/div[2]/p/span/text()’)
‘’‘with open(‘cccc’,‘w’,encoding=‘utf-8’) as f:
for i in kk:
print(i)
f.write(i)
‘’’
import pandas as pd
df=pd.DataFrame(kk)
df.to_csv(‘x.csv’)`
仔细想想应该是编码的问题 笔者使用的是utf-8 编码
使用GB18030后正常