当前位置:   article > 正文

python之scrapy:攻克技术点ASP.NET分页处理、request和response传参、pipeline区分传来Items_python scrapy aspx 分页 post

python scrapy aspx 分页 post

上面一篇介绍了scrapy抓取的大概架构,此篇文章针对一些技术问题进行展开说明。

一、如何处理ASP.NET分页 

我们还是深圳房地产信息系统为例, 

 

因为之前一直是写ASP.NET的,.NET很多控件都是通过拖拽实现。很多代码可以省去编写过程,都是自动生成的。这里的下一页操作就是通过自动生成的js代码,scrapy框架是不能执行JS代码。但我们清楚他执行了_doPostBack函数,我们再看_doPostBack是怎么定义的。 

  1. function __doPostBack(eventTarget, eventArgument) {
  2. if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
  3. theForm.__EVENTTARGET.value = eventTarget;
  4. theForm.__EVENTARGUMENT.value = eventArgument;
  5. theForm.submit();
  6. }
  7. }

来看看form1是怎么定义的 

  1. <form name="Form1" method="post" action="index.aspx?javascript%3a_doPostBack('AspNetPager1'%2c'2')" id="Form1">
  2. <input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="">
  3. <input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="">
  4. <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="dghA4AVaavTQEp48h3UsU0pxBkQIXyBt5k9xMnYc7lAPiW4jhB6DeTz+bLHqNiicT340W3E8oWRZP/u/oso9HuTTmRjM7qcpd2VbxKKgRY9CT0ZM3xlJEZLNtaTdBldZpfmozLsjBqdp/jzVFyHqrtybakbNR4CK2KXFmDJkIkynac0a5GIVwD0w2VYDmh40llArcntW86hbqVAUcLnh7aybgU5zdn0uuBbpxHJ/e8POBnkadJvDPV/zThfxPqsZs2wP9+NJL//WDUQQ6/exRnA3mdSsrOfeT1O6Tpl7Z0blgxzhBpOthPwgPdYBx8nLqeEoObijxvjFqQm8F8YaaElSJYJbsa8VRhd2dreBqDXItev6MlKHFy/TYA8CizrBIozDGzvVvZsBLNVxLvi8kVoYH7FvWF5Bf8WJ40ADdXwR6DSdbFFi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SYjvVd+AKBFpEHyCR4r+VvuP6OlRZqbTHHG45J+ZB92BA6eO2LgY41uVe2exi7gWvjP0FlXoFa6EDozxNZrj+jrxJ9K6w4r8VWZ/i3HnDHjNykDG/v9E2uR5ydnM0QSxuFPqDOrDZhFdmPgLjj1xY7o6sLUxXU9zdY1W4in+2Xa2pNrHgzsFdoKfM6p2TNtCp2HdLhijUGkm5NYjepBHpvyRGSVYNffwX8F/F3XcsXk5y4KOvaGeSi2Dy2AJUKtgbiy5tFE9inzZcOh+OBFlYFAwA5EEIQU6gTSYZ8zaZ0Ybw+7s9EUooqam9Dp4rv13AhAsA5WODEPHt25eXhrl7/8bGUggMojGU8sApfi6L/lfbsqZAh1hI6QHgLfwHLRXgnDxfylHgCs3FZXYWY58iOscfxt0gK1mU9EHySaQFNRzw8vUQ+jT/wfFIrEafPsMREPc1/eEHbKAdZVG92G/MDelrrj">

里面有很多hidden的控件,里面的值很多而且都是自动产生的,具体我也不太清楚什么意思。 

但是我们清楚,他的原理就是向本页面再次发送一次POST请求,进行分页。所以从URL地址上看没有变化。既然有POST操作,具体传了些什么参数。灵光一现,与其研究JS代码怎么封装,不如直接抓包看看传递什么参数。

 这里使用经常用到的wireshark,简单好用,清晰明了。

当我们点击下一步以后,我们可以看见发送了一个HTTP请求,执行了POST方法。

进一步展开来看。所有的参数,一一罗列。传递参数也就是页面上隐藏的。

 

回头再来看我写的分页代码,进行递归查询。

  1. def parse(self, response):
  2. context = response.xpath('//tr[@bgcolor="#F5F9FC"]/td[3]')
  3. dbhelp=RishomePipeline()
  4. for item in context:
  5. title=item.xpath('a/text()').extract_first()
  6. idstr=item.xpath('a/@href').extract_first()
  7. idstr=idstr[idstr.find('=')+1:]
  8. if dbhelp.ispropertyexits(idstr):
  9. return
  10. request=scrapy.Request(url='http://ris.szpl.gov.cn/bol/projectdetail.aspx?id='+idstr, method='GET',callback=self.showdetailpage)
  11. yield request
  12. '''以下是分页代码,组合post_data结构体,
  13. POST请求要使用 yield scrapy.FormRequest(url=response.url,formdata =post_data,callback=self.parse,dont_filter=True)函数。
  14. '''
  15. next_page = response.xpath('//*[@id="AspNetPager1"]/div[2]/a[3]/@href')
  16. pnum=next_page.extract_first().split(',')[1].replace("'","").replace(")","")
  17. post_data = {
  18. "__EVENTTARGET" : "AspNetPager1",
  19. "__EVENTARGUMENT" :pnum,
  20. "__VIEWSTATEENCRYPTED" : "",
  21. "tep_name" : "",
  22. "organ_name" : "",
  23. "site_address" : "",
  24. "AspNetPager1_input" : "1"}
  25. a = response.xpath('//*[@id="__VIEWSTATE"]/@value')
  26. post_data['__VIEWSTATE']=a.extract_first()
  27. b=response.xpath('//*[@id="__VIEWSTATEGENERATOR"]/@value')
  28. post_data['__VIEWSTATEGENERATOR']=b.extract_first()
  29. c = response.xpath('//*[@id="__EVENTVALIDATION"]/@value')
  30. post_data['__EVENTVALIDATION'] = c.extract_first()
  31. '''分页到最后一页,‘下一页’的按钮就不是链接了,页面没有href参数了,此时判断分页结束,即 递归结束'''
  32. if pnum is not None and pnum!="":
  33. yield scrapy.FormRequest(url=response.url,formdata =post_data,callback=self.parse,dont_filter=True)

 二、request和response之间如何传参

有些时候需要将两个页面的内容合并到一个item里面,这时候就需要在yield scrapy.Request的同时,传递一些参数到一下页面中。这时候可以这样操作。

  1. request=scrapy.Request(houseurl,method='GET',callback=self.showhousedetail)
  2. request.meta['biid']=biid
  3. yield request
  4. def showhousedetail(self,response):
  5. house=HouseItem()
  6. house['bulidingid']=response.meta['biid']

三、pipeline区分传来Items

各个页面都会封装items并将item传递给pipelines来处理,而pipelines接收的入口只有一个就是

def process_item(self, item, spider)函数

用来区分item的办法。

  1. def process_item(self, item, spider):
  2. if str(type(item))=="<class 'rishome.items.RishomeItem'>":
  3. self.saverishome(item)
  4. if str(type(item))=="<class 'rishome.items.BulidingItem'>":
  5. self.savebuliding(item)
  6. if str(type(item))=="<class 'rishome.items.HouseItem'>":
  7. self.savehouse(item)
  8. return item # 必须实现返回

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/438945
推荐阅读
相关标签
  

闽ICP备14008679号