scrapy爬虫框架(2. logging模块的使用、yield scrapy.Request()翻页请求)
程序员文章站
2022-04-22 13:08:56
...
1.logging模块的使用
1.1scrapy项目中的使用
1.settings.py中设置LOG_LEVEL=“WARNING”
2.settings.py中设置LOG_FILE="./log.log" #这是日志保存的位置,设置后终端就不会显示日志内容
3.程序里进行日志输出:
import logging
logger=logging.getLogger(__name__) #实例化logger
class spider(scrapy.Spider):
....
...
def parse(self,response):
...
...
logger.warning(item)
1.2普通项目
import logging
logging.basicConfig(…) #设置日志格式
logger=logging.getLogger(name) #实例化logger
在任何py文件中调用logger即可
2.翻页请求
翻页请求:
1.找到下一页地址
2.组装成一个Request对象交给引擎
2.1首先爬取腾讯课堂第一页的代码如下
import scrapy
class NextpageSpider(scrapy.Spider):
name = 'NextPage'
allowed_domains = ['qq.com']
start_urls = ['https://ke.qq.com/course/list?mt=1001&st=2002&tt=3019&price_min=1&page=1']
def parse(self, response):
datalist=response.xpath("//div[@class='market-bd market-bd-6 course-list course-card-list-multi-wrap js-course-list']//li")
for data in datalist:
item={}
if data.xpath(".//h4//a/text()").extract_first() !=None:
item["name"] = data.xpath(".//h4//a/text()").extract_first()
item["price"] = data.xpath(".//span[@class='line-cell item-price custom-string']/text()").extract_first()
item["number"] = data.xpath("normalize-space(.//span[@class='line-cell item-user custom-string']/text())").extract_first()
print(item)
2.2找到下页的路径,并且注意最后一页的下页路径和第一页的下页路径不一样,根据这个来决定停止
第一页时下页的url地址:
最后一页时的下页地址:
根据这个url地址就可以通过yield构造Request对象传给引擎
# -*- coding: utf-8 -*-
import scrapy
class NextpageSpider(scrapy.Spider):
name = 'NextPage'
allowed_domains = ['qq.com']
start_urls = ['https://ke.qq.com/course/list?mt=1001&st=2002&tt=3019&price_min=1&page=1']
def parse(self, response):
datalist=response.xpath("//div[@class='market-bd market-bd-6 course-list course-card-list-multi-wrap js-course-list']//li")
for data in datalist:
item={}
if data.xpath(".//h4//a/text()").extract_first() !=None:
item["name"] = data.xpath(".//h4//a/text()").extract_first()
item["price"] = data.xpath(".//span[@class='line-cell item-price custom-string']/text()").extract_first()
item["number"] = data.xpath("normalize-space(.//span[@class='line-cell item-user custom-string']/text())").extract_first()
print(item)
#找到下一页的url地址
next_url=response.xpath("//a[@class='page-next-btn icon-font i-v-right']/@href").extract_first()
if next_url != "javascript:void(0);" and next_url != None:
#通过yield把地址包装成Request请求传向引擎
yield scrapy.Request(next_url,callback=self.parse)
else:
print("end")
3.scrapy.Request知识点:
上一篇: 三种搜索求解八数码 问题的效率对比
下一篇: 利用scrapy爬取网页数据的初级实践