【问题标题】:Web scraping with bs4 outputs empty results使用 bs4 进行 Web 抓取输出空结果
【发布时间】:2019-11-08 21:38:40
【问题描述】:

我正在抓取,但我遇到了一个网页问题,它给了我空值(我正在寻找值 59.5 和 61) 这是代码

import requests
import lxml
from bs4 import BeautifulSoup

r = requests.get("https://dolarbalanz.com")
soup = BeautifulSoup(r.text, 'lxml')
info = soup.find_all("div", attrs={"class": "marco-dolarcobrar"})
print(info)

输出是一个字符串,它是应该包含我需要的值的 div 类。但是值的特定部分是空的('': '')。问题是什么?这是因为内容是动态的,我应该使用硒吗?如果是这种情况,如何从我的代码中做到这一点? 谢谢!

埃里克

【问题讨论】:

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


    【解决方案1】:

    此数据由 javascripts 呈现。 但是,如果您转到 nextwork 选项卡,您将看到下面的 API,它以 json 格式返回数据。

    https://dolarbalanz.com/api/dolarBalanz

    import requests
    r = requests.get("https://dolarbalanz.com/api/dolarBalanz").json()
    print(r['precioCompraVenta'][0]['precioventa'])
    print(r['precioCompraVenta'][0]['preciocompra'])
    

    输出

    61
    59.5
    

    网络标签

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-17
      • 2019-12-23
      • 1970-01-01
      • 2020-09-04
      • 2019-03-12
      • 2018-11-08
      • 2020-01-08
      • 1970-01-01
      相关资源
      最近更新 更多