python - 通过 Python 请求 POST URL 编码与基于行的文本数据

标签 python web-scraping mechanize wget scrape

我试图从网站上抓取一些数据,但无法让 POST 工作,它的表现就好像我没有向它提供输入数据(“appnote”)。

当我检查 POST 数据时,它看起来相对相同,只是实际的 Web 表单的 POST 被称为“URL 编码”并列出每个表单输入,而我的被标记为“基于行的文本数据”。

这是我的代码:(appnote)和搜索(Search)是我需要的最相关的部分

import requests
import cookielib


jar = cookielib.CookieJar()
url = 'http://www.vivotek.com/faq/'
headers = {'content-type': 'application/x-www-form-urlencoded'}

post_data = {#'__EVENTTARGET':'',
             #'__EVENTARGUMENT':'',
             '__LASTFOCUS':'',
             '__VIEWSTATE':'',
             '__VIEWSTATEGENERATOR':'',
             '__VIEWSTATEENCRYPTED':'',
             '__PREVIOUSPAGE':'',
             '__EVENTVALIDATION':''
             'ctl00$HeaderUc1$LanguageDDLUc1$ddlLanguage':'en',
             'ctl00$ContentPlaceHolder1$CategoryDDLUc1$DropDownList1':'-1',
             'ctl00$ContentPlaceHolder1$ProductDDLUc1$DropDownList1':'-1',
             'ctl00$ContentPlaceHolder1$Content':'appnote',
             'ctl00$ContentPlaceHolder1$Search':'Search'
            }
response = requests.get(url, cookies=jar)

response = requests.post(url, cookies=jar, data=post_data, headers=headers)

print(response.text)

链接到我在 Wireshark 中谈论的内容的图像:

我也尝试使用 wget 得到相同的结果。

最佳答案

主要问题是您没有设置重要的隐藏字段值,例如__VIEWSTATE

要使用请求来实现此功能,您需要解析页面 html 并获取适当的输入值。

这是使用 BeautifulSoup 的解决方案HTML 解析器和请求:

from bs4 import BeautifulSoup
import requests

url = 'http://www.vivotek.com/faq/'
query = 'appnote'

headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36'}

session = requests.Session()
response = session.get(url, headers=headers)

soup = BeautifulSoup(response.content)

post_data = {'__EVENTTARGET':'',
             '__EVENTARGUMENT':'',
             '__LASTFOCUS':'',
             '__VIEWSTATE': soup.find('input', id='__VIEWSTATE')['value'],
             '__VIEWSTATEGENERATOR': soup.find('input', id='__VIEWSTATEGENERATOR')['value'],
             '__VIEWSTATEENCRYPTED': '',
             '__PREVIOUSPAGE': soup.find('input', id='__PREVIOUSPAGE')['value'],
             '__EVENTVALIDATION': soup.find('input', id='__EVENTVALIDATION')['value'],

             'ctl00$HeaderUc1$LanguageDDLUc1$ddlLanguage': 'en',
             'ctl00$ContentPlaceHolder1$CategoryDDLUc1$DropDownList1': '-1',
             'ctl00$ContentPlaceHolder1$ProductDDLUc1$DropDownList1': '-1',
             'ctl00$ContentPlaceHolder1$Content': query,
             'ctl00$ContentPlaceHolder1$Search': 'Search'
            }

response = session.post(url, data=post_data, headers=headers)

soup = BeautifulSoup(response.content)
for item in soup.select('a#ArticleShowLink'):
    print item.text.strip()

打印 appnote 查询的具体结果:

How to troubleshoot when you can't watch video streaming?
Recording performance benchmarking tool
...

关于python - 通过 Python 请求 POST URL 编码与基于行的文本数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26131326/

相关文章:

Python Regex 从数据结构中提取多条数据

python - Flask 脚本有哪些优点?

python - 如何使用 MsgWaitForMultipleObjects 可靠地检测断开的 TCP 套接字?

python - NumPy 的 : convert labels into indexes

java - Jsoup如何返回页面内脚本生成的电子邮件ID

python - Mechanize 引发 BrowserStateError

python - 剥离 numpy 数组中的空格

python selenium 在循环中查找子元素

python - 如何使用scrapy从站点地图中抓取修改日期已更改的url?

perl - 如何在下载失败时不退出?