Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 来自终端和文件的请求的不同状态响应_Python_Python Requests_Python Requests Html_Postman Pre Request Script - Fatal编程技术网

Python 来自终端和文件的请求的不同状态响应

Python 来自终端和文件的请求的不同状态响应,python,python-requests,python-requests-html,postman-pre-request-script,Python,Python Requests,Python Requests Html,Postman Pre Request Script,从终端,命令响应。状态返回200 从myfile.py命令requests.get(下一页,headers=headers)返回 我希望从文件中获得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 Geck

从终端,命令
响应。状态
返回
200

myfile.py
命令
requests.get(下一页,headers=headers)
返回

我希望从文件中获得
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)

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


是的,在使用请求时,不要重复引用任何内容,包括参数和数据参数!
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'}