英文:
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(" ", "", 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
<Response [200]>
答案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 = '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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论