【问题标题】:beautiful soup findall not returning results美丽的汤 findall 没有返回结果
【发布时间】:2018-12-05 04:48:41
【问题描述】:

我正在尝试webscrape 并获得下面 html 中列出的保险金额。

保险 保险

使用了下面的代码,但它没有获取任何东西。有人可以帮忙吗?我对 python 还很陌生...

import requests

from bs4 import BeautifulSoup

r = requests.get('https://www.kbb.com/ford/escape/2017/s/?vehicleid=415933&intent=buy-new')

html_soup = BeautifulSoup(r.content, 'lxml')

test2 = html_soup.find_all('div',attrs={"class":"col-base-6"})

print(test2)

【问题讨论】:

    标签: html beautifulsoup


    【解决方案1】:

    并非您在页面上看到的所有数据实际上都是对该 URL 的 get 请求的响应。浏览器在后台还有很多其他的请求,都是由javascript代码发起的。

    具体来说,保险数据的请求是通过这个 URL 进行的:

    https://www.kbb.com/vehicles/hub/_costtoown/?vehicleid=415933
    

    这是您需要的工作代码:

    import requests
    from bs4 import BeautifulSoup
    
    r = requests.get('https://www.kbb.com/vehicles/hub/_costtoown/?vehicleid=415933')
    
    html_soup = BeautifulSoup(r.text, 'html.parser')
    
    Insurance = html_soup.find('div',string="Insurance").find_next().text
    
    print(Insurance)
    

    【讨论】:

    • 非常感谢 Moshe 解释和分享代码。我执行了代码并得到了预期的结果!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多