python爬虫数据写入csv文件中文乱码,用’utf-8‘在pycharm中打开文件没有问题,但是用excel打开却出现了问题,以下为解决方法。
(最近在练习爬虫,这个博文是对自己学习的记录和分享,如果有问题,非常感谢各路大佬指出!!!)
for r in result:
r_lis=[] #方法二:将爬到的内容储存在csv文件中
r_lis.append(r.text)
with open('company.csv','a',newline='',encoding='utf-8-sig') as csvfile:
spamwriter=csv.writer(csvfile, delimiter=',')
spamwriter.writerow(r_lis)
在with自开合那里加入newline='',encoding='utf-8-sig'是关键
以下是天眼查的爬虫
import requests
from bs4 import BeautifulSoup
from urllib.parse import quote
import csv #公司信息以csv的格式存储看起来更美观
# key='华为' #从查询一个company开始,加for循环,查询多个企业
lis=['华大基因','知乎','中国国家地理杂志社','中山大学']
for li in lis:
url='https://www.tianyancha.com/search?key='+quote(li) #外部的url,进入要查询的企业的页面
headers={
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36 Edg/93.0.961.52',
'Cookie':'aliyungf_tc=dcee015d38cbc96ad677b845beb17fecf55dfe7bf358e85e3369cb76604d9baa; acw_tc=76b20f8416320655286472268e3b9f535aea9900bb46a056331a772cc85ae5; csrfToken=ZguchyQQdbrVckAuPOspK7Au; jsid=SEO-BING-ALL-SY-000001; TYCID=c06be740195e11ec9d547d348b283d0a; ssuid=5077711456; sajssdk_2015_cross_new_user=1; bannerFlag=true; Hm_lvt_e92c8d65d92d534b0fc290df538b4758=1632065532; _ga=GA1.2.1408096384.1632065532; _gid=GA1.2.1583561769.1632065532; searchSessionId=1632065541.64432166; relatedHumanSearchGraphId=17066311; relatedHumanSearchGraphId.sig=TwOcKyCjpeHTjV7s49eE4L_3pHpB94avftKmZJek8gk; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%2218438709046%22%2C%22first_id%22%3A%2217bfeafae43ba9-005e2376b1979-57341f44-1382400-17bfeafae44afa%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%2C%22%24latest_referrer%22%3A%22%22%7D%2C%22%24device_id%22%3A%2217bfeafae43ba9-005e2376b1979-57341f44-1382400-17bfeafae44afa%22%7D; tyc-user-info={%22state%22:%220%22%2C%22vipManager%22:%220%22%2C%22mobile%22:%2218438709046%22}; tyc-user-info-save-time=1632066453345; auth_token=eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxODQzODcwOTA0NiIsImlhdCI6MTYzMjA2NjQ1MiwiZXhwIjoxNjYzNjAyNDUyfQ.XYcBGbzr1qThhAZp9exmEKjWg1Co6_cwyZgmfCNO-9Nrp_KSyyqAr--UnxUhLaaX7x6jNmoX3SACJDL31RocNA; tyc-user-phone=%255B%252218438709046%2522%255D; Hm_lpvt_e92c8d65d92d534b0fc290df538b4758=1632066573'
} #这里的Cookie需要是用户登陆后的才可以用,否则需要输入验证码,所以先登陆,再获取Cookie
html=requests.get(url,headers=headers)
soup=BeautifulSoup(html.text,'html.parser')
innerUrl=soup.select_one('a.name')['href'] #只需要的一个所以用select_one()来爬取一个内部的url#获取内部url即此处的href
# print('获取内部URL:', innerUrl)
innerHtml=requests.get(innerUrl,headers=headers) #这里的headers不需要变
soup=BeautifulSoup(innerHtml.text,'html.parser')
result=soup.select('.table.-striped-col tbody tr td')
for r in result:
# with open('company.text','a',encoding='utf8') as f: #方法一:将爬到的内容存储在text文本中
# f.write(r.text+'\n')
# with open('company.text','a',encoding='utf8') as f:
# f.write('\n')
r_lis=[] #方法二:将爬到的内容储存在csv文件中
r_lis.append(r.text)
with open('company.csv','a',newline='',encoding='utf-8-sig') as csvfile:
spamwriter=csv.writer(csvfile, delimiter=',')
spamwriter.writerow(r_lis)
print('已完成!')
这是本人的第一篇博文,这些内容主要是对我自己学习爬虫的记录和分享,如果代码中有什么问题的感谢各路大佬指正!!!