在python中发布帖子请求以便抓取

时间:2015-08-26 18:16:53

标签: python web-scraping

我的目标是在输入字段中的信息并点击提交后能够访问网站上的数据。我正在使用Httpfox来获取“发布”所需的值。我在代码下面加了一个截图。

#SECTION 1: import modules
import requests

#SECTION 2: setup variables

url = 'http://www.clarkcountynv.gov/Depts/assessor/Pages/PropertyRecords.aspx?H=redrock&P=assrrealprop/pcl.aspx'

ses = requests.session()

values = []

values.append({
    '__LASTFOCUS' : '',
    '__EVENTARGUMENT' : '',
    '__EVENTTARGET' : 'Submit1',
    '__VIEWSTATE' : '/wEPDwUJLTcyMjI2NjUwZBgBBR5fX0NvbnRyb2xzUmVxdWlyZVBvc3RCYWNrS2V5X18WAgUKY2hrQ3VycmVudAUKY2hrSGlzdG9yeUfXtwoelaE/eJmc1s9mHzvIqqwk',
    '__VIEWSTATEGENERATOR' : '5889EE07',
    '__EVENTVALIDATION' : '/wEWBQLnuv+BDALRqf+zDQK+zcmQBAK+zeWoCALVo8avDjkJwx8mhBoXL3mYGKBSY5lYBPxY',
    'hdnInstance' : 'pcl',
    'parcel' : '124-32-816-087', #this is what changes
    'age' : 'pcl17',
    'Submit1' : 'Submit'})

#SECTION 3: grab html text       
r = ses.post(url, data = values)
r.content() #this either throws an error 'too many values to unpack' or gives me the content of the main page if i play around with the input values a little, not the redirected page which is my problem

enter image description here

1 个答案:

答案 0 :(得分:1)

当您通过简单的GET获得相同的结果时,您不需要启动POST请求

最后一页的页面检查高亮显示用于显示实际搜索结果的iframe。

您可以直接从此网址获取结果,方法是替换" your-parcel-number-here"具有所需的值(在您的示例中为124-32-816-087)。

  

http://sandgate.co.clark.nv.us/assrrealprop/ParcelDetail.aspx?hdnParcel=your-parcel-number-here

看起来不需要cookie并且链接工作正常(我在firefox私有模式下尝试了该链接)。

相关问题