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

标签 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)

最佳答案

定义一个 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/

相关文章:

python - 2个列表之间的差异

python - 解析和聚合 Google Analytics 浏览器版本 CSV 数据

python - 如何比较同一元组列表中的出现次数

python - open()默认使用什么编码?

Python 请求 PUT 不起作用,但 cURL 起作用

从终端使用 SOCKS5 时 Python 的请求为 "Missing dependencies for SOCKS support"

python - 加入字符串列表的列表

python - 如何在 Python 中使用 OpenCV 存储网络摄像头视频

python - 抓取网站时回发或回调参数无效

python - 将参数传递给 requests.Session.get 引发 TypeError