Python的`request.get`在URL为’https://www.cnb.cz’的网页上不起作用。

huangapple go评论121阅读模式
英文:

Python request.get not working on url = 'https: // www.cnb.cz' webpage

问题

我尝试从以下地址获取字符串:

url = "https://www.cnb.cz/cs/financni-trhy/devizovy-trh/kurzy-devizoveho-trhu/kurzy-devizoveho-trhu/rok.txt?rok=2023"

然而,无法访问该页面。出现以下错误:

Lib\site-packages\requests\models.py", line 445, in prepare_url raise InvalidURL(f"Invalid URL {url!r}: No host supplied") requests.exceptions.InvalidURL: Invalid URL 'https://www.cnb.cz/cs/financni-trhy/devizovy-trh/kurzy-devizoveho-trhu/kurzy-devizoveho-trhu/rok.txt?rok=2023': No host supplied

我尝试了以下操作:

HEADERS = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
response = requests.get(url, headers=HEADERS, verify=False)

我还尝试了 https://www.cnb.cz/cs/,但没有成功。似乎这个页面不接受请求。

英文:

I tried to get string from:

url = "https: // www.cnb.cz / cs / financni - trhy / devizovy - trh / kurzy - devizoveho - trhu / kurzy - devizoveho - trhu / rok.txt?rok = 2023". 

However, it does not get access to the page. It turns:

Lib\site-packages\requests\models.py", line 445, in prepare_url raise InvalidURL(f"Invalid URL {url!r}: No host supplied") requests.exceptions.InvalidURL: Invalid URL 'https: // www.cnb.cz / cs / financni - trhy / devizovy - trh / kurzy - devizoveho - trhu / kurzy - devizoveho - trhu / rok.txt?rok = 2023': No host supplied

I tried:

HEADERS = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
response = requests.get(url,headers=HEADERS, verify=False)

I tried also the https://www.cnb.cz/cs/ without success. It seems that this page does not accept the request.

答案1

得分: 1

以下是已翻译的内容:

"你现在尝试的网址是不正确的,有太多的空格。"

以下是修复方法:

import re
import requests

url = re.sub(" ", "", url)
print(url)

requests.get(url)

输出:

https://www.cnb.cz/cs/financni-trhy/devizovy-trh/kurzy-devizoveho-trhu/kurzy-devizoveho-trhu/rok.txt?rok=2023

<Response [200]>
英文:

The url you're trying is incorrect, too many spaces.

Here's a way to fix that:

import re
import requests

url = re.sub(&quot; &quot;, &quot;&quot;, url)
print(url)

requests.get(url)

Output:

https://www.cnb.cz/cs/financni-trhy/devizovy-trh/kurzy-devizoveho-trhu/kurzy-devizoveho-trhu/rok.txt?rok=2023

&lt;Response [200]&gt;

答案2

得分: 1

它工作:

import requests

url = 'https://www.cnb.cz/cs/financni-trhy/devizovy-trh/kurzy-devizoveho-trhu/kurzy-devizoveho-trhu/rok.txt?rok=2023'

page = requests.get(url).content

print(page)

英文:

It works:

import requests

url = &#39;https://www.cnb.cz/cs/financni-trhy/devizovy-trh/kurzy-devizoveho-trhu/kurzy-devizoveho-trhu/rok.txt?rok=2023&#39;

page = requests.get(url).content

print(page)

huangapple
  • 本文由 发表于 2023年6月26日 02:41:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76551928.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定