【问题标题】:Python Web scrape a page that redirects to other pagePython Web抓取重定向到其他页面的页面
【发布时间】:2018-11-19 00:49:07
【问题描述】:

我在抓取网页内容时遇到了困难。

为了解释这一点,这是我的 Python 代码:

response = requests.post('http://a836-acris.nyc.gov/bblsearch/bblsearch.asp?borough=1&block=733&lot=66',{'User-Agent' : 'Mozilla/5.0'})

这给了我一个包含表单的 HTML 页面(不包含最终页面):

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Untitled Page</title>
</head>
<body>

    <form name="bbldata" action="https://a836-acris.nyc.gov/DS/DocumentSearch/BBLResult" method="post">
    <input type="hidden" name="hid_borough" value="1"/>
    <input type="hidden" name="hid_borough_name" value="MANHATTAN / NEW YORK" />
    <input type="hidden" name="hid_block" value="733"/>
    <input type="hidden" name="hid_block_value" value="733"/>
    <input type="hidden" name="hid_lot" value="66"/>
    <input type="hidden" name="hid_lot_value" value="66"/>
    <input type="hidden" name="hid_unit" value=""/>
    <input type="hidden" name="hid_selectdate" value=""/>
    <INPUT TYPE="HIDDEN" NAME="hid_datefromm"  VALUE="">
    <INPUT TYPE="HIDDEN" NAME="hid_datefromd"  VALUE="">
    <INPUT TYPE="HIDDEN" NAME="hid_datefromy"  VALUE="">
    <INPUT TYPE="HIDDEN" NAME="hid_datetom"  VALUE="">
    <INPUT TYPE="HIDDEN" NAME="hid_datetod"  VALUE="">
    <INPUT TYPE="HIDDEN" NAME="hid_datetoy"  VALUE="">
    <input type="hidden" name="hid_doctype" value=""/>
    <input type="hidden" name="hid_doctype_name" value="All Document Classes"/>
    <input type="hidden" name="hid_max_rows" value="10"/>
    <input type="hidden" name="hid_page" value="1" />
    <input type="hidden" name="hid_ReqID" value=""/>
    <input type="hidden" name="hid_SearchType" value="BBL"/>
    <input type="hidden" name="hid_ISIntranet" value="N"/>    
    <input type="hidden" name="hid_sort" value=""/>    
    </form>

<script language="JavaScript">
    document.bbldata.submit();
</script>
</body>
</html>

但是,如果你在浏览器中输入这个 url,你最终会在 HTML 中的脚本加载后得到这个网页,这必须被抓取:

任何帮助将不胜感激!

【问题讨论】:

    标签: python selenium web-scraping beautifulsoup python-requests


    【解决方案1】:

    您示例中的 HTML 表格显示了您需要发布的数据。我想您知道,您使用的 URL 实际上是 referer。所以,你需要:

    # 1. Create a payload
    payload = {
            'hid_borough': 1,
            'hid_borough_name': 'MANHATTAN / NEW YORK',
            'hid_block': 733,
            'hid_block_value': 733,
            'hid_lot': 66,
            'hid_lot_value': 66,
            'hid_doctype_name': 'All Document Classes',
            'hid_max_rows': 10,
            'hid_page': 1,
            'hid_SearchType': 'BBL',
            'hid_ISIntranet': 'N'
            }
    
    # 2. Add the correct referer to your headers
    header = {'User-Agent': 'Mozilla/5.0',
           'referer': 'http://a836-acris.nyc.gov/bblsearch/bblsearch.asp?borough=1&block=733&lot=66'}
    
    # 3. Add payload and headers to the post
    redirect = 'https://a836-acris.nyc.gov/DS/DocumentSearch/BBLResult'
    result = requests.post(redirect, data=payload, headers=header)
    
    print result.url
    https://a836-acris.nyc.gov/DS/DocumentSearch/BBLResult
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-09
      • 2018-07-10
      • 2017-05-01
      • 2012-03-16
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      相关资源
      最近更新 更多