Beautiful Soup 4 findall()与<img>标记中的元素不匹配

2024-10-02 04:25:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用BeautifulSoup4来帮助我从Imgur下载一张图片,尽管我怀疑Imgur部分是否相关。举个例子,我在这里使用网页:https://imgur.com/t/lenovo/mLwnorj

我的代码如下:

import webbrowser, time, sys, requests, os, bs4      # Not all libraries are used in this code snippet
from selenium import webdriver

browser = webdriver.Firefox()
browser.get("https://imgur.com/t/lenovo/mLwnorj")

res = requests.get(https://imgur.com/t/lenovo/mLwnorj)
res.raise_for_status()
soup = bs4.BeautifulSoup(res.text, features="html.parser")

imageElement = soup.findAll('img', {'class': 'post-image-placeholder'})
print(imageElement)

Imgur链接上的HTML代码包含如下部分:

<img alt="" src="//i.imgur.com/JfLsH5y.jpg" class="post-image-placeholder" style="max-width: 100%; min-height: 546px;" original-title="">

我通过使用Inspect元素中的point-and-click工具选择页面上的第一个图像元素找到了它。你知道吗

问题是imageElement中应该有两个项,每个图像一个,但是print函数显示[]。我也尝试过其他形式的soup.findAll('img', {'class': 'post-image-placeholder'}),比如soup.findall("img[class='post-image-placeholder']"),但没有什么不同。你知道吗

此外,当我使用

imageElement = soup.select("h1[class='post-title']")

,只是为了测试,print函数确实返回了一个匹配项,这让我怀疑它是否与标记有关。你知道吗

[<h1 class="post-title">Cable management increases performance. </h1>]

谢谢你的时间和努力


Tags: httpsimagecomimgrespostplaceholderclass
2条回答

如果网站将在页面加载后插入对象,则需要使用Selenium而不是requests。你知道吗

from bs4 import BeautifulSoup
from selenium import webdriver

url = 'https://imgur.com/t/lenovo/mLwnorj'
browser = webdriver.Firefox()
browser.get(url)
soup = BeautifulSoup(browser.page_source, 'html.parser')
images = soup.find_all('img', {'class': 'post-image-placeholder'})

[print(image['src']) for image in images]

# //i.imgur.com/JfLsH5yr.jpg
# //i.imgur.com/lLcKMBzr.jpg

这里的基本问题似乎是当第一次加载页面时实际的<img ...>元素不存在。在我看来,最好的解决方案是利用SeleniumWebDriver,您已经可以使用它来获取图像。Selenium将允许页面正确呈现(使用JavaScript和all),然后定位您关心的任何元素。你知道吗

例如:

import webbrowser, time, sys, requests, os, bs4      # Not all libraries are used in this code snippet
from selenium import webdriver

# For pretty debugging output
import pprint


browser = webdriver.Firefox()
browser.get("https://imgur.com/t/lenovo/mLwnorj")

# Give the page up to 10 seconds of a grace period to finish rendering
# before complaining about images not being found.
browser.implicitly_wait(10)

# Find elements via Selenium's search
selenium_image_elements = browser.find_elements_by_css_selector('img.post-image-placeholder')
pprint.pprint(selenium_image_elements)

# Use page source to attempt to find them with BeautifulSoup 4
soup = bs4.BeautifulSoup(browser.page_source, features="html.parser")

soup_image_elements = soup.findAll('img', {'class': 'post-image-placeholder'})
pprint.pprint(soup_image_elements)

我不能说我已经测试了这段代码,但是一般的概念应该有用。你知道吗


更新:

我继续进行测试,修复了代码中的一些错误,然后得到了我希望看到的结果:

Output from running code

相关问题 更多 >

    热门问题