import requests
import xlwings
from selenium import webdriver
def download_image(image_url,ASIN):
#下载图片
resp = requests.get(image_url, headers=headers)
file_path = 'E:/Download/BASIN/' + ASIN + '.jpg'
with open(file_path,'wb') as f:
f.write(resp.content)
def get_image_url(ASIN):
#获得图片地址
url = 'https://www.amazon.com/dp/' + ASIN
browser.get(url)
img_element = browser.find_element_by_xpath('.//div[@id="imgTagWrapperId"]/img')
image_url = img_element.get_attribute('data-old-hires')
return image_url
app = xlwings.App()
wb = app.books.open('E:/Download/BASIN/Casin.xlsx')
ws = wb.sheets('1')
ASINS = ws.range('a2:a3').value #打开excel文件读取需要爬取主图的asin
browser = webdriver.Chrome()
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36',
}
image_news = []
for ASIN in ASINS:
print(ASINS.index(ASIN)+1, ASIN)
try:
image_url = get_image_url(ASIN)
download_image(image_url,ASIN)
image_news.append('已下载')
except:
image_news.append('没找到')
ws.range('g2').options(transpose=True).value = image_news #在g列写下下载结果
wb.save()
app.quit()
browser.close()
上面的代码是我复制别人的来改的 就是改了文件路径
下面是报错提示
D:\Users\TY\AppData\Local\Programs\Python\Python310\python.exe C:/Users/TY/PycharmProjects/pythonProject5/main.py
Traceback (most recent call last):
File "C:\Users\TY\PycharmProjects\pythonProject5\main.py", line 22, in
ws = wb.sheets('1')
File "D:\Users\TY\AppData\Local\Programs\Python\Python310\lib\site-packages\xlwings\main.py", line 4893, in call
return Sheet(impl=self.impl(name_or_index))
File "D:\Users\TY\AppData\Local\Programs\Python\Python310\lib\site-packages\xlwings_xlwindows.py", line 877, in call
return Sheet(xl=self.xl(name_or_index))
File "D:\Users\TY\AppData\Local\Programs\Python\Python310\lib\site-packages\xlwings_xlwindows.py", line 208, in call
v = self._inner(*args, **kwargs)
File "C:\Users\TY\AppData\Local\Temp\gen_py\3.10\00020813-0000-0000-C000-000000000046x0x1x9.py", line 36625, in call
ret = self.oleobj.InvokeTypes(0, LCID, 2, (9, 0), ((12, 1),),Index
pywintypes.com_error: (-2147352567, '发生意外。', (0, None, None, None, 0, -2147352565), None)
进程已结束,退出代码1
ASIN在表格sheet1种
有没有人帮我改下啊 谢谢