简体   繁体   English

Python http 请求返回无意义的响应

[英]Python http request returns meaningless response

I'm trying to build a simple web scraper with Python by using the 'cfscrape' and 'requests' modules:我正在尝试使用“cfscrape”和“请求”模块构建一个简单的 web 刮板和 Python:

import cfscrape
import requests

url = "my-url"

headers = {
    "Accept": "application/json",
    "Referer": url,
    "Host": "host",
    "User-Agent": "my-user-agent",
    "Accept-Language": "en-GB",
    "Accept-Encoding": "gzip, deflate, br",
    "Connection": "keep-alive",
}

session = requests.session()
scraper = cfscrape.create_scraper(sess=session)
r = scraper.get(url, headers=headers)

print(r)
print("******************")
print(r.text)

Howerver, what I get is this strange output:但是,我得到的是这个奇怪的 output:

在此处输入图像描述

As you can see, the request returns a successful response (statusCode: 200).如您所见,请求返回成功响应(statusCode:200)。 However, the response content is completely meaningless.但是,响应内容完全没有意义。

How can I solve the problem?我该如何解决这个问题? Thank you.谢谢你。

You did not provide content of headers variable.您没有提供headers变量的内容。 So I just ommited it and run your code against URL wich returns JSON:所以我只是省略了它并针对 URL 运行您的代码,它返回 JSON:

import cfscrape
import requests

url = "https://api.aktuality.sk/v1.0/tools/namedays"

session = requests.session()
scraper = cfscrape.create_scraper(sess=session)
r = scraper.get(url)

print(r)
print("******************")
print(r.text)

and I received JSON correctly.我正确收到了 JSON。 I am using:我在用:

cfscrape-2.1.1 
python 3.8.5

but from what you said, you are calling not URL but you are loading JSON data from file, so assumed from picture, what you see is in binary.但从你所说的来看,你不是在调用 URL 而是从文件加载 JSON 数据,所以从图片中假设,你看到的是二进制。 I think you should read file and then you would get JSON from file.我认为你应该阅读文件,然后你会从文件中得到 JSON。 Something like this for local file:本地文件是这样的:

import json 
  
# Opening JSON file 
f = open('data.json',) 
  
# returns JSON object as  
# a dictionary 
data = json.load(f) 
  
print(data) 
  
# Closing file 
f.close() 

Or if it's remote file:或者如果它是远程文件:

import urllib.request, json 

with urllib.request.urlopen("https://api.aktuality.sk/v1.0/tools/namedays") as url:
    data = json.loads(url.read().decode())
    print(data)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM