python爬取图片并写入excel
python爬取图片并下载到本地文件夹,python爬取图片并写入excel文件
·
目标:
1)python爬取图片并下载到本地文件夹
2)python爬取图片并写入到excel文件
Ⅰ.python程序
# 导入库
import requests
import parsel
import xlsxwriter
# 请求头
headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36'}
# 创建excel,设置列宽
wb=xlsxwriter.Workbook('电影海报.xlsx')
ws=wb.add_worksheet('海报')
ws.set_column('A:A',7)
ws.set_column(1,2,50)
# 标题行
headings=['海报','名称','链接']
# 设置excel风格
ws.set_tab_color('red')
head_format=wb.add_format({'bold':1,'fg_color':'cyan','align':'center','font_name':u'微软雅黑','valign':'vcenter'})
cell_format=wb.add_format({'bold':0,'align':'center','font_name':u'微软雅黑','valign':'vcenter'})
ws.write_row('A1',headings,head_format)
j=0
# 发送请求,获取响应,遍历电影信息
for i in range(0,250,25):
url='https://movie.douban.com/top250?start='+str(i)
response=requests.get(url,headers=headers)
response.encoding=response.apparent_encoding
selector=parsel.Selector(response.text)
lis=selector.css('#content>div>div.article>ol>li>div>div.pic>a>img::attr(src)').getall()
title=selector.css('#content>div>div.article>ol>li>div>div.info>div.hd>a>span:nth-child(1)::text').getall()
href=selector.css('#content>div>div.article>ol>li>div>div.pic>a::attr(href)').getall()
# 下载海报信息到本地
for n in range(len(lis)):
img=requests.get(lis[n]).content
with open(f'./海报/{title[n]}.jpg','wb') as f:
f.write(img)
# 将获取到的各种标签信息写入excel
for k in range(len(lis)):
ws.set_row(k+1+j*25,60)
ws.insert_image('A'+str(k+2+j*25),f'./海报/{title[k]}.jpg',{'x_scale':0.2,'y_scale':0.2})
ws.write(k+1+j*25,1,title[k],cell_format)
ws.write(k+1+j*25,2,href[k],cell_format)
j+=1
wb.close()
Ⅱ.效果
更多推荐
已为社区贡献2条内容
所有评论(0)