使用请求在 Python 中搜索网站时出现 502 错误

Der*_*ang 5 python python-requests

使用非常基本的程序在网站上搜索查询并打印出搜索结果,为什么会出现 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)
Run Code Online (Sandbox Code Playgroud)

ele*_*cay 7

定义一个User-Agent标题。像这样:

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)
Run Code Online (Sandbox Code Playgroud)

为什么有这个要求?维基媒体用户代理政策