使用请求在 Python 中搜索网站时出现 502 错误 [英] 502 error using Requests to search website in Python

查看:24
本文介绍了使用请求在 Python 中搜索网站时出现 502 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用一个非常基本的程序在网站上搜索查询并打印出搜索结果,为什么会出现 502 错误?

Using a very basic program to search up a query on a website and print out the search results, why do I get a 502 error?

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 标头.像这样:

Define a User-Agent header. Like this:

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)

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

这篇关于使用请求在 Python 中搜索网站时出现 502 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆