时间:2021-07-01 10:21:17 帮助过:8人阅读
同步分页
同步分页时,页面整体刷新,url地址栏会发生变化
爬虫解析的数据对象是html
测试场景:抓取某招聘网站北京区的Java职位
#coding=utf-8import scrapyclass TestSpider(scrapy.Spider): name='test' download_delay=3 user_agent='Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36' page_url = 'http://www.test.com/zhaopin/Java/{0}/?filterOption=2' page=1 #执行入口 def start_requests(self): #第一页 yield scrapy.Request(self.page_url.format('1'), headers={'User-Agent':self.user_agent}, callback=self.parse, errback=self.errback_httpbin) #解析返回的数据 def parse(self,response): for li in response.xpath('//*[@id="s_position_list"]/ul/li'): yield{ 'company':li.xpath('@data-company').extract(), 'salary':li.xpath('@data-salary').extract() } #是否是最后一页,根据下一页的按钮css样式判断 if response.css('a.page_no.pager_next_disabled'): print('---is the last page,stop!---') pass else: self.page=self.page+1 #抓取下一页 yield scrapy.Request(self.page_url.format(str(self.page)), headers={'User-Agent':self.user_agent}, callback=self.parse, errback=self.errback_httpbin) #异常处理 def errback_httpbin(self,failure): if failure.check(HttpError): response = failure.value.response print 'HttpError on {0}'.format(response.url) elif failure.check(DNSLookupError): request = failure.request print'DNSLookupError on {0}'.format(request.url) elif failure.check(TimeoutError, TCPTimedOutError): request = failure.request print'TimeoutError on {0}'.format(request.url)
启动爬虫:scrapy runspider //spiders//test_spider.py -o test.csv 完毕后生成csv格式的文件:
异步分页
异步分页时,页面局部刷新,url地址栏不发生变化
爬虫解析的数据对象通常是Json
测试场景:抓取某电影网站的经典电影前100
#coding=utf-8import scrapyimport jsonclass TestSpider(scrapy.Spider): name ='test' download_delay = 3 user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36' pre_url = 'https://movie.douban.com/j/search_subjects?type=movie&tag=%E7%BB%8F%E5%85%B8&sort=recommend&page_limit=20&page_start=' page=0 cnt=0 def start_requests(self): url= self.pre_url+str(0*20) yield scrapy.Request(url,headers={'User-Agent':self.user_agent},callback=self.parse) def parse(self,response): if response.body: # json字符串转换成Python对象 python_obj=json.loads(response.body) subjects=python_obj['subjects'] if len(subjects)>0: for sub in subjects: self.cnt=self.cnt+1 yield { 'title':sub["title"], 'rate':sub["rate"] } if self.cnt<100: print 'next page-------' self.page=self.page+1 url= self.pre_url+str(self.page*20) yield scrapy.Request(url,headers={'User-Agent':self.user_agent},callback=self.parse)
启动爬虫:scrapy runspider //spiders//test_spider.py -o test.json 完毕后生成json格式的文件:
Scrapy与BeautifulSoup or lxml的区别
scrapy是编写爬虫和抓取数据的一整套框架,而BeautifulSoup or lxml只是解析html/xml的库,功能就像scrapy的xpath和css选择器,所以它们也可以在scrapy下使用,只是运行效率相对较低。 在使用scrapy的选择器时,我们可以借助浏览器的F12模式,直接Copy任意节点的xpath和css值。