通过Python请求发布URL编码与基于行的文本数据

通过Python请求发布URL编码与基于行的文本数据,python,web-scraping,mechanize,wget,scrape,Python,Web Scraping,Mechanize,Wget,Scrape,我试图从一个网站上搜集一些数据,但我无法让帖子正常运行,这就像我没有给它输入数据一样(“appnote”) 当我检查POST数据时,它看起来相对相同,只是实际Web表单的POST被称为“URL编码”,并列出每个表单输入,而我的被标记为“基于行的文本数据” 这是我的代码:(appnote)和搜索(Search)是我需要的最相关的部分 import requests import cookielib jar = cookielib.CookieJar() url = 'http://www.vi

我试图从一个网站上搜集一些数据,但我无法让帖子正常运行,这就像我没有给它输入数据一样(“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进行了尝试,获得了相同的结果。

主要问题是没有设置重要的隐藏字段值,如
\uu VIEWSTATE

为了使用
请求
,您需要解析页面html并获得适当的输入值

下面是使用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
...

这真是太好了,谢谢你!所以,我想我缺少的主要是会话数据。这是有道理的!