【问题标题】:Different status responses of requests from terminal and file终端和文件请求的不同状态响应
【发布时间】:2020-06-20 23:52:15
【问题描述】:

从终端命令response.status 返回200

myfile.py 命令requests.get(next_page, headers=headers) 返回<Response [403]>

我希望从文件中获得200。有什么问题?

myfile.py:

import lxml.html as parser
import requests

headers = {'User-Agent': '"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36"'}
start_urls = [
#"https://www.vivareal.com.br/aluguel/?__vt=vt:b#banheiros=2&preco-ate=2500&preco-desde=1000&quartos=2"
"https://www.zapimoveis.com.br/aluguel/apartamentos/pe+recife/2-quartos/?__zt=ldt%3Ab&onde=,Pernambuco,Recife,,,,BR%3EPernambuco%3ENULL%3ERecife,-8.0522404,-34.9286096&banheiros=2&quartos=2&transacao=Aluguel&vagas=1&precoMaximo=2500&precoMinimo=1000&tipoUnidade=Residencial,Apartamento&tipo=Im%C3%B3ve^X^X^X^XsdsDSl%20usado&pagina=1"
]

next_page = start_urls[0]
print(next_page)
r = requests.get(next_page, headers=headers)
print(r)

h = parser.fromstring(r.text)
address = h.xpath('//p[contains(@class,"address")]/text()')
print(address)

【问题讨论】:

    标签: python python-requests python-requests-html postman-pre-request-script


    【解决方案1】:

    用户代理请求标头似乎有问题。 试试这个

    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.76 Safari/537.36'}
    

    【讨论】:

    • 是的,在使用请求时千万不要双引号,包括参数和数据参数!
    猜你喜欢
    • 2018-11-29
    • 1970-01-01
    • 1970-01-01
    • 2015-05-28
    • 1970-01-01
    • 2017-10-31
    • 2012-03-01
    • 2021-12-06
    • 2021-11-14
    相关资源
    最近更新 更多