我正在尝试从Amazon刮取图像,这并不容易。
我想我快到了,但是我没有得到结果。
在这里,我使用硒来1.打开主图像,然后单击2.在缩略图3中单击第二个图像,然后获取第二个图像的完整大小的src。
但是失败了,我不知道为什么
这是我写的台词。
from urllib.request import urlretrieve
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
driver = webdriver.Firefox()
url = "https://www.amazon.com/Kraft-Original-Macaroni-Microwaveable-Packets/dp/B005ECO3H0"
driver.get(url)
action = ActionChains(driver)
time.sleep(5)
driver.find_element_by_css_selector('#landingImage').click()
time.sleep(10)
html = driver.page_source
soup = BeautifulSoup(html,"html.parser")
driver.find_element_by_css_selector('#ivImage_1').click()
amazon = soup.select_one(".fullscreen")
imgUrl = amazon.find("img")['src']
print(imgUrl)
我无法理解的一件事是,如果键入print(amazon),它将为我提供img标签,但是根据上面代码的结果,imgUrl为'Nonetype'。
请帮助我找到答案。
干得好
from urllib.request import urlretrieve
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
driver = webdriver.Firefox()
url = "https://www.amazon.com/Kraft-Original-Macaroni-Microwaveable-
Packets/dp/B005ECO3H0"
driver.get(url)
action = ActionChains(driver)
time.sleep(5)
driver.find_element_by_css_selector('#landingImage').click()
time.sleep(5)
html = driver.page_source
soup = BeautifulSoup(html,"html.parser")
driver.find_element_by_css_selector('#ivImage_1').click()
image_url = driver.find_element_by_class_name("fullscreen").get_attribute("src")
print(image_url)
#if you want to download
import requests
resp = requests.get(image_url)
with open("asd.png", "wb")as image:
image.write(resp.content)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句