使用一个非常基本的程序在网站上搜索查询并打印出搜索结果,为什么会出现 502 错误?
import requests
from bs4 import BeautifulSoup
import re
def main():
url = "https://www.last10k.com/Search"
dat = {'q':'goog'}
resp = requests.get(url, params=dat)
print(resp.content)
最佳答案
定义一个 User-Agent
header 。像这样:
import requests
def main():
url = "https://www.last10k.com/Search"
dat = {'q':'goog'}
resp = requests.get(url, params=dat, headers={'User-Agent': 'Mozilla/5.0'})
print(resp.status_code)
为什么有这个要求? Wikimedia User-Agent policy
关于python - 使用请求在 Python 中搜索网站时出现 502 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43239698/