03 python38的scrapy处理json格式数据
创始人
2025-01-17 03:04:06
0

1 json文档爬取或者获取ajax请求的json格式数据

1.0 网站分析

#网址:http://www.cwl.gov.cn/ygkj/wqkjgg/ssq/ #列表页分析: 第一页:http://www.cwl.gov.cn/cwl_admin/front/cwlkj/search/kjxx/findDrawNotice?name=ssq&issueCount=&issueStart=&issueEnd=&dayStart=&dayEnd=&pageNo=1&pageSize=30&week=&systemType=PC 第二页:http://www.cwl.gov.cn/cwl_admin/front/cwlkj/search/kjxx/findDrawNotice?name=ssq&issueCount=&issueStart=&issueEnd=&dayStart=&dayEnd=&pageNo=2&pageSize=30&week=&systemType=PC 最后一页:http://www.cwl.gov.cn/cwl_admin/front/cwlkj/search/kjxx/findDrawNotice?name=ssq&issueCount=&issueStart=&issueEnd=&dayStart=&dayEnd=&pageNo=542&pageSize=30&week=&systemType=PC  请求方式get 参数修改pageNo 返回数据为json  

1.1 创建项目

scrapy startproject doubanSpider  cd ssqSpider scrapy genspider douban douban.com 

1.2 创建爬虫

scrapy genspider douban "douban.com" 

1.3 添加爬虫模型

import scrapy  class DoubanMovieItem(scrapy.Item):     # define the fields for your item here like:     # name = scrapy.Field()     title = scrapy.Field() #标题        stars = scrapy.Field() #分数        subject = scrapy.Field() #主题        pass  

1.4 修改爬虫代码,列表页的多页数据为例子

1.5.1 数据为json文档时使用response.json()

import scrapy from scrapy.http import HtmlResponse from scrapy import Request from ssqSpider.items import SsqspiderItem   class SsqSpider(scrapy.Spider):     name = "ssq"     allowed_domains = ["www.cwl.gov.cn"]     start_urls = ["https://cwl.gov.cn"]     # start_urls = ["http://www.cwl.gov.cn/cwl_admin/front/cwlkj/search/kjxx/findDrawNotice?name=ssq&issueCount=&issueStart=&issueEnd=&dayStart=&dayEnd=&pageNo=1&pageSize=30&week=&systemType=PC"]      def start_requests(self):         for page in range(1,55):   ## 1-54页,range为左闭右开             url=f'http://www.cwl.gov.cn/cwl_admin/front/cwlkj/search/kjxx/findDrawNotice?name=ssq&issueCount=&issueStart=&issueEnd=&dayStart=&dayEnd=&pageNo={page}&pageSize=30&week=&systemType=PC'                     yield Request(url=url,dont_filter=True)  # 如果有302跳转加入dont_filter=True      def parse(self, response:HtmlResponse):                # print(response.json())                data=response.json()         result=data['result']         for i in result:             item=SsqspiderItem()             # print(i)             item['qihao']=i['code']             item['riqi']=i['date']             item['kaijianghaoma_red']=i['red']             item['kaijianghaoma_blue']=i['blue']                          item['jiangchijiner']=i['poolmoney']             item['xiaoshouer']=i['sales']              yield item   

1.5.2 测试运行爬虫

scrapy crawl douban  #正式运行 或者 scrapy crawl douban -o douban.csv  

相关内容

热门资讯

透视ai!wepokerplu... 透视ai!wepokerplus开挂,wpk官网下载链接,方针教程(发现有挂)-哔哩哔哩1、完成wp...
七分钟外挂!aapoker怎么... 七分钟外挂!aapoker怎么拿好牌(透视)一直存在有挂,辅助教程(有挂讲解)-哔哩哔哩1、全新机制...
透视安装!wepoker私人局... 透视安装!wepoker私人局开挂视频(透视)一直真的有挂,实用技巧(有挂辅助)-哔哩哔哩wepok...
透视工具!拱趴大菠萝辅助方法,... 透视工具!拱趴大菠萝辅助方法,wepokerplus透视脚本免费,窍门教程(有挂方式)-哔哩哔哩1、...
7分钟专业!德州透视脚本(透视... 7分钟专业!德州透视脚本(透视)其实是有挂,黑科技教程(有挂技巧)-哔哩哔哩德州透视脚本辅助器中分为...
透视安卓版!wepokerpl... 透视安卓版!wepokerplus透视脚本免费(透视)一贯存在有挂,高科技教程(有挂透视)-哔哩哔哩...
透视ai代打!wepoker破... 透视ai代打!wepoker破解游戏盒子,aapoker插件下载,学习教程(有挂工具)-哔哩哔哩1、...
五分钟透视!wepoker私人... 五分钟透视!wepoker私人局规律(透视)总是存在有挂,攻略方法(确实有挂)-哔哩哔哩所有人都在同...
透视app!菠萝德州透视脚本(... 透视app!菠萝德州透视脚本(透视)原来是真的挂,力荐教程(有挂私人房)-哔哩哔哩1、进入到菠萝德州...
透视辅助!hhpoker有没有... 透视辅助!hhpoker有没有辅助辅助,智星德州插件最新版本更新内容详解,方式教程(详细教程)-哔哩...