首页 > 解决方案 > 来自终端和文件的请求的不同状态响应

问题描述

命令从终端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)

标签: pythonpython-requestspython-requests-htmlpostman-pre-request-script

解决方案


看起来用户代理请求标头有问题。尝试这个

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'}

推荐阅读