【问题标题】:BeautifulSoup does not show some tags in html pageBeautifulSoup 没有在 html 页面中显示一些标签
【发布时间】:2019-08-20 22:50:50
【问题描述】:

如果我访问此页面here,我可以在检查时看到带有img标签的页面上的图像。

但是当我尝试使用requests 获取页面并使用BeautifulSoup 进行解析时,我无法访问相同的图像。我在这里错过了什么?

代码运行良好,我从请求中得到 200 作为 status_code。

import requests
from bs4 import BeautifulSoup

url = 'https://mangadex.org/chapter/435396/2'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.76 Safari/537.36'}

page = requests.get(url,headers=headers)
print(page.status_code)

soup = BeautifulSoup(page.text,'html.parser')
img_tags = soup.find_all('img')
for img in img_tags:
    print(img)

编辑::

根据建议,硒选项工作正常。但是有没有办法像 BeautifulSoup 那样加快速度?

【问题讨论】:

  • @SuperShoot 这确实按预期工作,但速度很慢。有更快的方法吗?喜欢,无需访问该页面。
  • PhantomJS 会加快速度吗?
  • 通常如果页面是用 JavaScript 加载的,有一个 api 你可以直接调用 request 来获取 JSON 格式的数据。查看页面检查的网络选项卡,查看正在使用哪些资源将数据拉入页面。
  • @SuperShoot 非常感谢,分析网络调用给了我API请求。如何接受您的评论?

标签: python python-3.x web-scraping beautifulsoup


【解决方案1】:

该页面包含需要运行的 JavaScript 才能填充页面上的某些元素。您可以在访问图像之前使用Selenium 运行页面的 JavaScript。

【讨论】:

    【解决方案2】:

    您可以使用 API 来获取图像。下面的代码从页面中获取所有图像并打印 url:

    import requests
    
    headers = {
        'Accept': 'application/json, text/plain, */*',
        'Referer': 'https://mangadex.org/chapter/435396/2',
        'DNT': '1',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) '
                      'AppleWebKit/537.36 (KHTML, like Gecko) '
                      'Chrome/73.0.3683.86 Safari/537.36',
    }
    
    params = (
        ('id', '435396'),
        ('type', 'chapter'),
        ('baseURL', '/api'),
    )
    
    response = requests.get('https://mangadex.org/api/', headers=headers, params=params)
    data = response.json()
    
    img_base_url = "https://s4.mangadex.org/data"
    img_hash = data["hash"]
    img_names = data["page_array"]
    
    for img in img_names:
        print(f"{img_base_url}/{img_hash}/{img}")
    

    输出:

    https://s4.mangadex.org/data/ac081a99e13d8765d48e55869cd5444c/x1.png
    https://s4.mangadex.org/data/ac081a99e13d8765d48e55869cd5444c/x2.png
    https://s4.mangadex.org/data/ac081a99e13d8765d48e55869cd5444c/x3.png
    https://s4.mangadex.org/data/ac081a99e13d8765d48e55869cd5444c/x4.png
    https://s4.mangadex.org/data/ac081a99e13d8765d48e55869cd5444c/x5.png
    https://s4.mangadex.org/data/ac081a99e13d8765d48e55869cd5444c/x6.png
    https://s4.mangadex.org/data/ac081a99e13d8765d48e55869cd5444c/x7.png
    https://s4.mangadex.org/data/ac081a99e13d8765d48e55869cd5444c/x8.png
    https://s4.mangadex.org/data/ac081a99e13d8765d48e55869cd5444c/x9.png
    https://s4.mangadex.org/data/ac081a99e13d8765d48e55869cd5444c/x10.png
    https://s4.mangadex.org/data/ac081a99e13d8765d48e55869cd5444c/x11.png
    https://s4.mangadex.org/data/ac081a99e13d8765d48e55869cd5444c/x12.png
    https://s4.mangadex.org/data/ac081a99e13d8765d48e55869cd5444c/x13.png
    https://s4.mangadex.org/data/ac081a99e13d8765d48e55869cd5444c/x14.png
    https://s4.mangadex.org/data/ac081a99e13d8765d48e55869cd5444c/x15.png
    https://s4.mangadex.org/data/ac081a99e13d8765d48e55869cd5444c/x16.png
    @ 987654337@
    https://s4.mangadex.org/data/ac081a99e13d8765d48e55869cd5444c/x18.png
    https://s4.mangadex.org/data/ac081a99e13d8765d48e55869cd5444c/x19.png
    https://s4.mangadex.org/data/ac081a99e13d8765d48e55869cd5444c/x20.png
    https://s4.mangadex.org/data/ac081a99e13d8765d48e55869cd5444c/x21.png
    https://s4.mangadex.org/data/ac081a99e13d8765d48e55869cd5444c/x22.png
    https://s4.mangadex.org/data/ac081a99e13d8765d48e55869cd5444c/x23.png
    https://s4.mangadex.org/data/ac081a99e13d8765d48e55869cd5444c/x24.png
    https://s4.mangadex.org/data/ac081a99e13d8765d48e55869cd5444c/x25.png
    https://s4.mangadex.org/data/ac081a99e13d8765d48e55869cd5444c/x26.png
    https://s4.mangadex.org/data/ac081a99e13d8765d48e55869cd5444c/x27.png
    https://s4.mangadex.org/data/ac081a99e13d8765d48e55869cd5444c/x28.png
    https://s4.mangadex.org/data/ac081a99e13d8765d48e55869cd5444c/x29.png
    https://s4.mangadex.org/data/ac081a99e13d8765d48e55869cd5444c/x30.png
    https://s4.mangadex.org/data/ac081a99e13d8765d48e55869cd5444c/x31.png
    https://s4.mangadex.org/data/ac081a99e13d8765d48e55869cd5444c/x32.png
    https://s4.mangadex.org/data/ac081a99e13d8765d48e55869cd5444c/x33.png
    https://s4.mangadex.org/data/ac081a99e13d8765d48e55869cd5444c/x34.png
    https://s4.mangadex.org/data/ac081a99e13d8765d48e55869cd5444c/x35.png
    https://s4.mangadex.org/data/ac081a99e13d8765d48e55869cd5444c/x36.png

    【讨论】:

    • 这正是我通过@SuperShoot 的评论得出的结论。感谢您提供简洁的代码。
    • 这个 API 的使用有什么限制?
    • @Doc 我不知道。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-12
    • 1970-01-01
    • 2015-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-26
    相关资源
    最近更新 更多