【问题标题】:BeautifulSoup isn't returning a url when we query for the src of the img tag当我们查询 img 标签的 src 时,BeautifulSoup 没有返回 url
【发布时间】:2020-07-28 21:54:57
【问题描述】:
from bs4 import BeautifulSoup
from urllib import request

url = "https://amazon-asin.com/asincheck/?product_id=B000JMLBHU"
req = request.urlopen(url)
soap = BeautifulSoup(req,'html.parser')
soap.find('img',{'class':'resp-img'})['ng-src']

我正在使用 ng-src,因为只有“src”,它什么也不返回。但是,使用 ng-src,它会返回:

'{{data.product_details.image_url}}'

为什么它不返回网址?如何抓取这张图片的网址?

【问题讨论】:

  • url是用javascript加载的
  • 如果您满意,请投票

标签: web-scraping beautifulsoup request urllib


【解决方案1】:

试试这个:

from selenium import webdriver
driver = webdriver.Firefox(executable_path='c:program/geckodriver')
url = "https://amazon-asin.com/asincheck/?product_id=B000JMLBHU"

driver.get(url)
driver.implicitly_wait(10)
print(driver.find_element_by_css_selector('img.resp-img').get_attribute('ng-src'))
driver.close()

打印:

https://m.media-amazon.com/images/I/51sPuWd2JbL.jpg

注意你需要seleniumgeckodriver,并且在这段代码中geckodriver设置为从c:/program/geckodriver.exe导入

【讨论】:

    猜你喜欢
    • 2020-12-01
    • 2012-11-26
    • 2018-01-07
    • 2021-10-16
    • 1970-01-01
    • 2020-07-21
    • 2019-06-03
    • 2017-10-14
    • 2013-04-06
    相关资源
    最近更新 更多