【问题标题】:Request for data that generates chart always empty生成图表的数据请求始终为空
【发布时间】:2023-02-06 23:00:24
【问题描述】:

我正在尝试使用 python 的请求模块来抓取在网站上生成图表的数据。这是有问题的图表:

我的代码目前看起来像这样:

# load modules
import os
import json
import requests as r

# url to send the call to
postURL = "<insert website>"

# utiliz get to pull cookie data
cookie_intel = r.get(postURL, verify = False)

# get cookies
search_cookies = cookie_intel.cookies

#### Request Information ####

# API request data
post_data = <insert request data>

# header information
headers = {"user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"}

# results 
results_post = r.post(postURL, data = post_data, cookies = search_cookies, headers = headers, verify = False)

# result
print(results_post.json())

作为一个快速总结,我首先加载站点然后检查它,从那里我在网络选项卡中确定请求的 url,然后在有效负载选项卡中检查所需的请求数据。然后我从请求标头选项卡中获取了用户代理。

请求本身有效,但是它始终为空。我试过改变各种输入但没有成功。我将非常感谢任何有助于我解决此问题的提示。先感谢您!

【问题讨论】:

    标签: python web-scraping python-requests


    【解决方案1】:

    在这种情况下,根据请求 documentation 发出发布请求时,您必须使用 json= 而不是 data= 。通过替换这部分代码,您应该会得到预期的响应。

    results_post = r.post(postURL, json = post_data, cookies = search_cookies, headers = headers, verify = False)
    

    您也可以尝试使用其他抓取工具,如Scrapy 来抓取这些数据,并可能使用estela 在云端运行抓取工具。

    【讨论】:

      猜你喜欢
      • 2017-07-31
      • 1970-01-01
      • 2022-01-11
      • 2021-02-01
      • 1970-01-01
      • 2015-12-24
      • 1970-01-01
      • 2011-07-18
      • 2017-05-12
      相关资源
      最近更新 更多