【问题标题】:Web Scraping Text that is missing in the HTMLHTML 中缺少的 Web 抓取文本
【发布时间】:2019-08-21 14:52:46
【问题描述】:

我正在尝试从一系列表格中收集一些信息。大多数在线表单都将响应文本编码到 HTML 中,但是,有一个部分似乎并非如此。有没有办法仍然检索信息?

下面的链接是一个例子。具体来说,我正在查看“来源国家/地区”部分。像“意大利”和“西班牙”这样的国家名称不会出现在 HTML 中。因此,我无法将 Selenium 和 BS4 结合使用。 https://232app.azurewebsites.net/Forms/ExclusionRequestItem/13700

【问题讨论】:

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


    【解决方案1】:

    数据在页面中,但是以Javascript数组的形式。您可以使用 rejson 模块提取它,例如:

    import re
    import json
    import requests
    
    url = 'https://232app.azurewebsites.net/Forms/ExclusionRequestItem/13700'
    
    html_data = requests.get(url).text
    
    json_data = json.loads(re.findall(r'function createSourceCountriesTable\(\).*?var arrValues = (.*?);', html_data, flags=re.DOTALL)[0])
    
    print(json.dumps(json_data, indent=4))
    

    打印:

    [
        {
            "OriginCountry": "Spain",
            "ExportCountry": "Italy",
            "ExclusionQty": "20000",
            "Manufacturer": "Rodacciai",
            "Supplier": null
        },
        {
            "OriginCountry": "Spain",
            "ExportCountry": "Spain",
            "ExclusionQty": "3000",
            "Manufacturer": "Aceros Inoxidables Olarra",
            "Supplier": null
        },
        {
            "OriginCountry": "United Kingdom",
            "ExportCountry": "Italy",
            "ExclusionQty": "3000",
            "Manufacturer": "Rodacciai",
            "Supplier": null
        }
    ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-10
      • 2014-08-19
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-16
      • 2021-02-22
      相关资源
      最近更新 更多