Python Scraper 无法抓取 img src

问题描述 投票:0回答:2

我无法从 www.kissmanga.com 网站上抓取图像。我正在使用 Python3 以及 Requests 和 Beautifulsoup 库。抓取的图像标签给出空白的“src”。

SRC:

from bs4 import BeautifulSoup
import requests

scraper = cfscrape.create_scraper()

url = "http://kissmanga.com/Manga/Bleach/Bleach-634--Friend-004?id=235206"

response = requests.get(url)

soup2 = BeautifulSoup(response.text, 'html.parser')

divImage = soup2.find('div',{"id": "divImage"})

for img in divImage.findAll('img'):
     print(img)

response.close()

我认为图像抓取被阻止,因为我相信该网站使用了 cloudflare。基于这个假设,我还尝试使用“cfscrape”库来抓取内容。

python web-scraping beautifulsoup python-requests cloudflare
2个回答
3
投票

您需要等待

JavaScript
注入图像的
html
代码。

有多种工具可以做到这一点,以下是其中一些:

我能够让它与 Selenium 一起工作:

from bs4 import BeautifulSoup

from selenium import webdriver
from selenium.common.exceptions import TimeoutException

driver = webdriver.Firefox()
# it takes forever to load the page, therefore we are setting a threshold
driver.set_page_load_timeout(5)

try:
    driver.get("http://kissmanga.com/Manga/Bleach/Bleach-634--Friend-004?id=235206")
except TimeoutException:
    # never ignore exceptions silently in real world code
    pass

soup2 = BeautifulSoup(driver.page_source, 'html.parser')
divImage = soup2.find('div', {"id": "divImage"})

# close the browser 
driver.close()

for img in divImage.findAll('img'):
    print img.get('src')

如果您也想下载这些图片,请参阅如何使用请求下载图片


0
投票

您是否尝试过设置自定义用户代理? 这样做通常被认为是不道德的,但抄袭漫画也是如此。

© www.soinside.com 2019 - 2024. All rights reserved.