如何用python读取POST请求内容

2024-10-03 06:27:37 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,当我试图发送一个邮政请求,我得到了200状态码,我可以阅读页码.text但是当我无法通过xpath查找元素时
对不起,英语不好

import requests
from lxml import html
cookies = {
    '_ga': 'GA1.2.2008948104.1563397982',
    'kfacommnet': 'f5e38c05-1e26-42ce-8794-afbded5e6ee1',
    'kfasession': '1rgbtjwd4dhjkbi3lyrwyzhk',
    'cookie_matching': 'true',
    'pushNotification-shownCount-2195': '15',
    'pushNotification-notWantPopUp-2195': 'true',
    'tlc': 'true',
    '_gid': 'GA1.2.109054603.1563596637',
}

headers = {
    'Origin': 'http://www.kanoon.ir',
    'Accept-Encoding': 'gzip, deflate',
    'Accept-Language': 'en-GB,en;q=0.9,fa-IR;q=0.8,fa;q=0.7,fr-CH;q=0.6,fr;q=0.5,en-US;q=0.4',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36',
    'Content-Type': 'application/json; charset=UTF-8',
    'Accept': '*/*',
    'Referer': 'http://www.kanoon.ir/Public/SuperiorsRankBased?type=3',
    'X-Requested-With': 'XMLHttpRequest',
    'Connection': 'keep-alive',
}

data = '{"dept":"1","sahmieh":"2","rank":"345","reshte":null,"year":"97","univercity":null,"type":"3"}'
response = requests.post('http://www.kanoon.ir/Public/SuperiorsRankBasedShowSuperiors', headers=headers, cookies=cookies, data=data, verify=False)
page=response.text
tree = html.fromstring(page)
img = tree.xpath('//*[@id="header_area"]/div[1]/a/img')
print(img)

Tags: textimporttruehttpimgdatairwww
1条回答
网友
1楼 · 发布于 2024-10-03 06:27:37

page=response.text之后添加这样的print语句print(page),检查print语句的输出,并通过查看输出来更正xpath。因为您指定的当前xpath不存在。你知道吗

相关问题 更多 >