源代码
import requests
import parsel
import csv
for page in range(1,101):
print(f'\n======正在抓取第{page}页数据======')
url = f'https://km.lianjia.com/ershoufang/a4/pg{page}/'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'}
response = requests.get(url=url, headers=headers)
# print(response)
html_data = response.text
# print(html_data)
selector = parsel.Selector(html_data)
lis = selector.css('.clear.LOGCLICKDATA')
for li in lis:
title = li.css('.title a::text').get()
address = li.css('.positionInfo a::text').getall()
address = '_'.join(address)
introduce = li.css('.houseInfo::text').get()
star = li.css('.followInfo::text').get()
tags = li.css('.tag span::text').getall()
tags = ','.join(tags)
totalPrice = li.css('.totalPrice.totalPrice2 span::text').get() + '万'
unitPrice = li.css('.unitPrice span::text').get()
print(title, address, introduce, tags, totalPrice, unitPrice, sep='*****')
with open('链家2.csv',mode='a',encoding='utf-8',newline='') as f:
csv.write = csv.writer(f)
csv.write.writerow([title,address,introduce,tags,totalPrice,unitPrice])
运行过程没有出错,在程序内也显示正常
最后点开导出文件报错
该如何导出正确中文呢
我这复制你的代码运行后得到的文件打开是正常的,代码没有错,是你打开的方式出错啦
excel软件
试试 encoding='utf_8_sig'
你不指定encoding='utf-8'试试呢