【问题标题】:Web Scraping - Can't take a text valueWeb Scraping - 无法获取文本值
【发布时间】:2021-11-17 05:20:41
【问题描述】:

我正在尝试通过 python 抓取,已经尝试过:

  • bs4,硒,请求,氦。 [包括类、标签、xpath 等]..

虽然我从来没有遇到过这种问题,但也许我做错了什么。 我就是不能接受这个文本值:G120-5,如下图所示:

链接:https://www.kavak.com/br/carros-usados-100550

如果有人可以做到,请告诉我。在此先感谢:)

【问题讨论】:

    标签: python web-scraping


    【解决方案1】:

    您看到的数据是从外部 URL 加载的。你可以使用requests模块来模拟它:

    import json
    import requests
    
    
    # url = "https://www.kavak.com/br/carros-usados-100550"
    car_id = "100550"  # <-- this is the id from URL
    api_url = (
        f"https://api.kavak.services/services-common/inventory/{car_id}/dynamic"
    )
    
    headers = {
        "kavak-country-id": "76",
        "kavak-region-id": "4",
        "kavak-subsidiary-id": "3",
    }
    
    data = requests.get(api_url, headers=headers).json()
    # uncomment to print all data:
    # print(json.dumps(data, indent=4))
    
    print(data["data"]["coordinate"])
    

    打印:

    G120-5
    

    【讨论】:

    • 你介意解释一下你做了什么吗?例如,这行是什么: f"api.kavak.services/services-common/inventory{car_id}/dynamic 以及标题,您如何/在哪里找到 api.kavak.services
    • @renan f"https://api.kavak.services/services-common/inventory/{car_id}/dynamic" 在 Python 中被称为 f-string(你可以在 StackOverflow 上查看)。我在 Firefox 开发人员工具 -> 网络选项卡(Chrome 有类似的东西)中找到了正确的标题 - 页面正在执行所有请求。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-18
    • 1970-01-01
    相关资源
    最近更新 更多