求教为何python写入csv文件,用txt方式打开,发现头尾各多了一个双引号"!谢谢!

#目标:用多协程爬到薄荷网的食物热量数据。
from gevent import monkey
monkey.patch_all

from bs4 import BeautifulSoup
from gevent.queue import Queue
import gevent,time,csv,requests

#时间戳
sta=time.time()

#c创建文档
file=open(r’food_calorie1.csv’,‘w’,newline=’’,encoding=‘utf-8’)
writer=csv.writer(file)

#获得网址,创建队列,添加网址
work=Queue()
i=‘view_menu’
for j in range(1,4):
url=‘http://www.boohee.com/food/’+str(i)+’?page=’+str(j)
work.put_nowait(url)
for i in range(1,4):
for j in range(1,4):
work.put_nowait(url)
print(len(work))

#定义函数,获得数据,解析数据
def crawler():
while not work.empty():
url=work.get_nowait()
raw_headers=’’‘Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,/;q=0.8,application/signed-exchange;v=b3;q=0.9
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.9
Cache-Control: max-age=0
Connection: keep-alive
Cookie: Hm_lvt_7263598dfd4db0dc29539a51f116b23a=1582730871; Hm_lpvt_7263598dfd4db0dc29539a51f116b23a=1582789371
Host: www.boohee.com
If-None-Match: W/“a0cb03e4a0f2cc41604f1d79ed253f5b”
Referer: http://www.boohee.com/food/group/1
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36’’’
headers=dict([line.split(": “,1) for line in raw_headers.split(”\n")])
res=requests.get(url,headers=headers)
print(res.status_code)
foods=BeautifulSoup(res.text,‘html.parser’).find_all(class_=‘item clearfix’)
food=’’
for i in foods:
name=i.find_all(‘a’)[1][‘title’]
calo=i.find(‘p’).text
href_part=i.find_all(‘a’)[1][‘href’]
href=‘http://www.boohee.com/shiwu’+href_part
# food+=’’’
# -----------------------
# 食物:{}
# {}
# 链接:{}
# ‘’’.format(name,calo,href)
food+=’-----------------------\n食物:%s\n%s\n链接:%s\n’%(name,calo,href)
writer.writerow([food])

#创建任务,添加任务,执行任务
tasks=[]
for i in range(4):
task=gevent.spawn(crawler)
tasks.append(task)
gevent.joinall(tasks)

#时间戳,计算耗时
end=time.time()
print(end-sta)

以下是用记事本打开csv文件的样子
在这里插入图片描述

相关推荐
©️2020 CSDN 皮肤主题: 游动-白 设计师:白松林 返回首页