【问题标题】:Python parsing with bs4使用 bs4 进行 Python 解析
【发布时间】:2018-06-30 05:01:15
【问题描述】:
import requests
from bs4 import BeautifulSoup
r=requests.get("https://www.pexels.com/photo/sunglasses-sunset-summer-sand-
46710/")
cont=r.content
soup = BeautifulSoup(cont,"html.parser")
img=soup.findAll('src')
print(img)
我不能有 src 文本,我的代码返回空字符串,我该如何解决?
【问题讨论】:
标签:
python-3.x
beautifulsoup
request
【解决方案1】:
我想您正试图从链接中获取眼镜图片。您至少应该发布源代码。但无论如何,在您的代码中,您试图从 HTML 中获取“src”,即使没有名为“src”的标签。它属于一个“img”标签,所以这就是你应该寻找的,然后是 src。我查看了 URL,您要查找的图像具有 image-section__image 类。您可以使用它来查找您的图像。我会发布sn-p:
soup = BeautifulSoup(r.content, 'lxml')
img = soup.find('img', class_='image-section__image')
print(img['src'])