【问题标题】:Unable to scrape drop down menu using BeautifulSoup and Requests无法使用 BeautifulSoup 和 Requests 抓取下拉菜单
【发布时间】:2020-02-06 02:29:38
【问题描述】:

我想在百年灵网站上的产品页面上抓取各种信息。

示例页面:https://www.breitling.com/gb-en/watches/navitimer/b01-chronograph-46/AB0127211C1A1/

我在刮掉“添加到袋子”按钮上方的下拉菜单中给出的手表表带材料时遇到问题(示例中的“钢 1.4435”)。

我想要的具体元素是:

<small class="dd-selected-description dd-desc dd-selected-description-truncated">Steel 1.4435</small>

但是,这不会在对我的 GET 请求的响应中返回。最接近&lt;small&gt; 标记的元素是带有id='strap-selector-list'&lt;div&gt; 元素。

但是,当调用 soup.find(id='strap-selector-list') 时,它会显示 &lt;div&gt; 不包含任何内容。

import requests
from bs4 import BeautifulSoup

url = 'https://www.breitling.com/gb-en/watches/navitimer/b01-chronograph-46/AB0127211C1A1/'

r = requests.get(url)
soup = BeautifulSoup(r.text, 'lxml')

soup.find(id='strap-selector-list')

返回

<div id="strap-selector-list"></div>

我怎样才能获得里面的信息(如打开检查器时显示的那样?)

Screenshot of page with inspector open highlighting areas of interest

我尝试过的:

  1. 欺骗标头。我在开发人员工具的“网络”选项卡中复制/粘贴了所有请求标头(cookie 除外)。我在 GET 请求中使用了它们(为简洁起见,仅包括更改的行)
headers = {'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'en-GB,en-US;q=0.9,en;q=0.8',
'cache-control': 'max-age=0',
'dnt': '1',
'referer': 'https://www.breitling.com/gb-en/watches/navitimer/?search%5Bref%5D=&search%5Bsorting%5D=newest',
'sec-fetch-mode': 'navigate, same-origin, cors',
'sec-fetch-site': 'same-origin',
'sec-fetch-user': '?1',
'upgrade-insecure-requests': '1',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36',
'X-Requested-With': 'XMLHttpRequest'
}

r = requests.get(url, headers=headers)

  1. 已检查 XHR 请求。页面加载时只有 3 个。一个是结帐篮的状态,一个提供零售商的信息,例如他们的商店位置,另一个是 status.php,它给出 404 错误。

    如果您单击下拉菜单,则不会发送任何 XHR 请求。

    如果您单击下拉菜单中的任何项目,您将被带到该项目的产品页面。

  2. 使用不同的解析器,例如html.parser 没有区别

  3. 在标头中添加 cookie 并执行正常的 GET 请求,也没有区别
  4. 首先创建session = requests.Session() 并在有和没有headers=headers 的情况下执行r = session.get(url) 也不起作用。

非常感谢任何帮助!

【问题讨论】:

    标签: python web-scraping beautifulsoup python-requests


    【解决方案1】:

    您要查找的数据位于script 元素下。

    您需要做的就是加载作为脚本主体返回的 JSON 并遍历字典。

    import requests
    from bs4 import BeautifulSoup
    import json
    import pprint
    
    url = 'https://www.breitling.com/gb-en/watches/navitimer/b01-chronograph-46/AB0127211C1A1/'
    
    r = requests.get(url)
    soup = BeautifulSoup(r.text, 'html')
    
    script = soup.find(id='app-reference-versions')
    pprint.pprint(json.loads(script.contents[0]))
    

    输出

    https://pastebin.com/kGhMQt61

    【讨论】:

    • @DarkLeader 我正在扫描页面源代码,寻找像“1.4435”这样的词
    • 是的,我在 html 中找到了下拉菜单的位置,但像 OP 一样,我无法获取内容,也没有找到任何 id 标签
    • @DarkLeader 做“查看源代码”并寻找script type="application/json"
    • 如何查看源代码?
    • @DarkLeader 是的,控制 + F 并输入 type="application/json"app-reference-versions。然后你会看到script 元素
    猜你喜欢
    • 1970-01-01
    • 2021-09-13
    • 2019-09-07
    • 2021-11-02
    • 2020-04-22
    • 1970-01-01
    • 2015-04-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多