反扒 post 浏览器可获取数据 scrapy数据为空
目标地址:http://www.ccgp-gansu.gov.cn/web/article/128/0/index.htm
此网站为post提交,返回html文本,详细的可以看我的代码
想爬取的内容:列表中的项目
问题:scrapy获取的body中没有列表的数据 ul中没有li
曾尝试解决,用cookiejar:True,还是没有数据
希望有能力的小伙伴,能给予一点提示,不胜感激
spider源文件
```
# -*- coding: utf-8 -*-
import re
import scrapy
import scrapy_splash
from demo.items import DemoItem
from datetime import datetime
class GgzyfwSpider(scrapy.Spider):
name = 'gsccgp'
allowed_domains = ['http://www.ccgp-gansu.gov.cn']
start_urls = ['http://www.ccgp-gansu.gov.cn/web/doSearchmxarticle.action']
url = 'http://www.ccgp-gansu.gov.cn/web/doSearchmxarticle.action'
def get_form_data(self, page):
payload = {'articleSearchInfoVo.releasestarttime': '',
'articleSearchInfoVo.releaseendtime': '',
'articleSearchInfoVo.tflag': '1',
'articleSearchInfoVo.classname': '128',
'articleSearchInfoVo.dtype': '0',
'articleSearchInfoVo.days': '',
'articleSearchInfoVo.releasestarttimeold': '',
'articleSearchInfoVo.releaseendtimeold': '',
'articleSearchInfoVo.title': '',
'articleSearchInfoVo.agentname': '',
'articleSearchInfoVo.bidcode': '',
'articleSearchInfoVo.proj_name': '',
'articleSearchInfoVo.buyername': '',
'total': '5402',
'limit': '20',
'current': str(page),
'sjm': '7466'}
return payload
def start_requests(self):
yield scrapy_splash.SplashFormRequest(method='post', formdata=self.get_form_data(1),
url=self.url, callback=self.parse)
def parse(self, response):
tr_list = response.xpath("//ul[@class='Expand_SearchSLisi']/li")
if not tr_list:
return
else:
pass
current = self.settings.get('CURRENT_DATA')
domain = 'http://www.ccgp-gansu.gov.cn'
# 第一个tr是表头
for li in tr_list:
date_str = li.xpath("string(.//span[1]//text())").get().strip()
# 开标时间: | 发布时间:2020-03-12 20:41:01 | 采购人:平凉市崆峒区白水镇人民政府 | 代理机构:甘肃海天建设工程造价咨询有限公司
date_arr = date_str.split('|')
date = date_arr[1].split(':')[1].strip()
buy_person = date_arr[2].split(':')[1].strip()
middle_name = date_arr[3].split(':')[1].strip()
if date:
# project_time
date_time = datetime.strptime(date, "%Y-%m-%d %H:%M:%S")
now_time = datetime.now()
diff_day = (now_time - date_time).days
if diff_day > current:
# 因为还要处理图片所以不能停止爬虫,停止了图片就也不处理了
# self.crawler.engine.close_spider(self, '日期过了')
# print('>>>>>日期过了')
return
else:
# print('>>>>>可以继续')
pass
item = DemoItem()
item['publish_date'] = date
item['source_url'] = self.start_urls[0]
item['project_name'] = li.xpath(".//a//text()").get()
href = li.xpath('.//a/@href').get()
item['url'] = domain + href
# 废标/终止公告 | 平凉市崆峒区白水镇人民政府 | 农、林、牧、渔业
other_str = li.xpath("string(.//span/strong//text())").get().strip()
other_arr = other_str.split('|')
item['status'] = other_arr[0].strip()
item['buy_area'] = other_arr[1].strip()
item['project_type_name'] = other_arr[2].strip()
item['buy_person'] = buy_person
item['middle_name'] = middle_name
print(item)
# yield item
depth = response.meta.get('depth', 0)
page = depth + 1
url = domain + '/web/doSearchmxarticle.action?limit=20&start=' + str(page * 20)
yield scrapy.Request(url=url, callback=self.parse)
```
#web网站post反扒#
目标地址:http://www.ccgp-gansu.gov.cn/web/article/128/0/index.htm
此网站为post提交,返回html文本,详细的可以看我的代码
想爬取的内容:列表中的项目
问题:scrapy获取的body中没有列表的数据 ul中没有li
曾尝试解决,用cookiejar:True,还是没有数据
希望有能力的小伙伴,能给予一点提示,不胜感激
spider源文件
```
# -*- coding: utf-8 -*-
import re
import scrapy
import scrapy_splash
from demo.items import DemoItem
from datetime import datetime
class GgzyfwSpider(scrapy.Spider):
name = 'gsccgp'
allowed_domains = ['http://www.ccgp-gansu.gov.cn']
start_urls = ['http://www.ccgp-gansu.gov.cn/web/doSearchmxarticle.action']
url = 'http://www.ccgp-gansu.gov.cn/web/doSearchmxarticle.action'
def get_form_data(self, page):
payload = {'articleSearchInfoVo.releasestarttime': '',
'articleSearchInfoVo.releaseendtime': '',
'articleSearchInfoVo.tflag': '1',
'articleSearchInfoVo.classname': '128',
'articleSearchInfoVo.dtype': '0',
'articleSearchInfoVo.days': '',
'articleSearchInfoVo.releasestarttimeold': '',
'articleSearchInfoVo.releaseendtimeold': '',
'articleSearchInfoVo.title': '',
'articleSearchInfoVo.agentname': '',
'articleSearchInfoVo.bidcode': '',
'articleSearchInfoVo.proj_name': '',
'articleSearchInfoVo.buyername': '',
'total': '5402',
'limit': '20',
'current': str(page),
'sjm': '7466'}
return payload
def start_requests(self):
yield scrapy_splash.SplashFormRequest(method='post', formdata=self.get_form_data(1),
url=self.url, callback=self.parse)
def parse(self, response):
tr_list = response.xpath("//ul[@class='Expand_SearchSLisi']/li")
if not tr_list:
return
else:
pass
current = self.settings.get('CURRENT_DATA')
domain = 'http://www.ccgp-gansu.gov.cn'
# 第一个tr是表头
for li in tr_list:
date_str = li.xpath("string(.//span[1]//text())").get().strip()
# 开标时间: | 发布时间:2020-03-12 20:41:01 | 采购人:平凉市崆峒区白水镇人民政府 | 代理机构:甘肃海天建设工程造价咨询有限公司
date_arr = date_str.split('|')
date = date_arr[1].split(':')[1].strip()
buy_person = date_arr[2].split(':')[1].strip()
middle_name = date_arr[3].split(':')[1].strip()
if date:
# project_time
date_time = datetime.strptime(date, "%Y-%m-%d %H:%M:%S")
now_time = datetime.now()
diff_day = (now_time - date_time).days
if diff_day > current:
# 因为还要处理图片所以不能停止爬虫,停止了图片就也不处理了
# self.crawler.engine.close_spider(self, '日期过了')
# print('>>>>>日期过了')
return
else:
# print('>>>>>可以继续')
pass
item = DemoItem()
item['publish_date'] = date
item['source_url'] = self.start_urls[0]
item['project_name'] = li.xpath(".//a//text()").get()
href = li.xpath('.//a/@href').get()
item['url'] = domain + href
# 废标/终止公告 | 平凉市崆峒区白水镇人民政府 | 农、林、牧、渔业
other_str = li.xpath("string(.//span/strong//text())").get().strip()
other_arr = other_str.split('|')
item['status'] = other_arr[0].strip()
item['buy_area'] = other_arr[1].strip()
item['project_type_name'] = other_arr[2].strip()
item['buy_person'] = buy_person
item['middle_name'] = middle_name
print(item)
# yield item
depth = response.meta.get('depth', 0)
page = depth + 1
url = domain + '/web/doSearchmxarticle.action?limit=20&start=' + str(page * 20)
yield scrapy.Request(url=url, callback=self.parse)
```
#web网站post反扒#