【问题标题】:POST URL Encoded vs Line-based text data via Python Requests通过 Python 请求 POST URL 编码与基于行的文本数据
【发布时间】:2014-09-30 22:38:42
【问题描述】:

我正在尝试从网站上抓取一些数据,但我无法让 POST 工作,就好像我没有给它输入数据(“appnote”)一样。

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

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

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 进行了尝试,结果相同。

【问题讨论】:

    标签: python web-scraping mechanize wget scrape


    【解决方案1】:

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

    要使用requests,您需要解析页面 html 并获取适当的输入值。

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

    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
    ...
    

    【讨论】:

    • 效果很好,谢谢!所以,我想我缺少的主要是会话数据。这是有道理的!
    猜你喜欢
    • 2013-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-10
    • 1970-01-01
    • 1970-01-01
    • 2014-07-05
    • 1970-01-01
    相关资源
    最近更新 更多