使用BeautifulSoup获取Booklooker页面高清图片链接的问题求助
问题分析
你当前代码的核心问题是定位错了元素:
- 你抓取的是
preview hasXXL类的<a>标签,这个标签的href是图片预览跳转页的链接,并非高清图直链。 - 高清图的真实直链存储在页面内图片元素的
data-xxl属性中。
修正后的代码
from bs4 import BeautifulSoup import requests url = "https://www.booklooker.de/B%C3%BCcher/Donna-W-Cross+Die-P%C3%A4pstin/id/A02A8f9001ZZl" page = requests.get(url) souped = BeautifulSoup(page.content, "html.parser") # 提取带有data-xxl属性的图片链接(高清图直链) for img in souped.find_all("img", attrs={"data-xxl": True}): print(img["data-xxl"])
备选实现方式
如果想基于你原本定位的preview hasXXL标签来提取,可通过其内部的<img>元素获取data-xxl属性:
from bs4 import BeautifulSoup import requests url = "https://www.booklooker.de/B%C3%BCcher/Donna-W-Cross+Die-P%C3%A4pstin/id/A02A8f9001ZZl" page = requests.get(url) souped = BeautifulSoup(page.content, "html.parser") for preview_link in souped.find_all(class_="preview hasXXL"): target_img = preview_link.find("img") if target_img and "data-xxl" in target_img.attrs: print(target_img["data-xxl"])
内容的提问来源于stack exchange,提问作者Benedikt Faude
相关产品推荐
相关产品推荐

