python - 来自终端和文件的请求的不同状态响应
问题描述
命令从终端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)
解决方案
看起来用户代理请求标头有问题。尝试这个
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'}
推荐阅读
- c++ - linux服务器中的Recv缓冲区大小
- java - HAPI 2 v2.3 - setValidating(false) 对 XMLParser 没有影响
- c# - 如何从数据库中检索 Checkedlistbox 值
- javascript - 多个关注 ID 导致 Twitter 流 API 出现状态 401 错误
- asp.net-core - 如何在 EF Core 中保存来自另一个 DbContext 的相关数据?
- java - 在 Lombok 中将字符串 Bean 资源作为 AllArgsConstructor 传递
- php - 带日期时间的 Highchart 面积图
- javascript - Angular 4中动态生成的仪表抛出“无法读取null的属性'getContext'”错误
- python - 如何使用熊猫测量 XY 点之间的距离
- powershell - 如何获取“需要 SSL”的 IIS 选项值?