如何在python中使用scrapy爬虫库-创新互联
这期内容当中小编将会给大家带来有关如何在python中使用scrapy爬虫库,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
10多年的扎囊网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。全网营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整扎囊建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联从事“扎囊网站设计”,“扎囊网站推广”以来,每个客户项目都认真落实执行。首先我们得安装scrapy框架
pip install scrapy
接着使用scrapy命令创建一个爬虫项目:
scrapy startproject questions
相关文件简介:
scrapy.cfg: 项目的配置文件
questions/: 该项目的python模块。之后您将在此加入代码。
questions/items.py: 项目中的item文件.
questions/pipelines.py: 项目中的pipelines文件.
questions/settings.py: 项目的设置文件.
questions/spiders/: 放置spider代码的目录.
questions/spiders/xueersi.py: 实现爬虫的主体代码.
xueersi.py 爬虫主体
# -*- coding: utf-8 -*- import scrapy import time import numpy import re from questions.items import QuestionsItem class xueersiSpider(scrapy.Spider): name = "xueersi" # 爬虫名字 allowed_domains = ["tiku.xueersi.com"] # 目标的域名 # 爬取的目标地址 start_urls = [ "http://tiku.xueersi.com/shiti/list_1_1_0_0_4_0_1", "http://tiku.xueersi.com/shiti/list_1_2_0_0_4_0_1", "http://tiku.xueersi.com/shiti/list_1_3_0_0_4_0_1", ] levels = ['偏易','中档','偏难'] subjects = ['英语','语文','数学'] # 爬虫开始的时候,自动调用该方法,如果该方法不存在会自动调用parse方法 # def start_requests(self): # yield scrapy.Request('http://tiku.xueersi.com/shiti/list_1_2_0_0_4_0_39',callback=self.getquestion) # start_requests方法不存在时,parse方法自动被调用 def parse(self, response): # xpath的选择器语法不多介绍,可以直接查看官方文档 arr = response.xpath("//ul[@class='pagination']/li/a/text()").extract() total_page = arr[3] # 获取分页 for index in range(int(total_page)): yield scrapy.Request(response.url.replace('_0_0_4_0_1',"_0_0_4_0_"+str(index)),callback=self.getquestion) # 发出新的请求,获取每个分页所有题目 # 获取题目 def getquestion(self,response): for res in response.xpath('//div[@class="main-wrap"]/ul[@class="items"]/li'): item = QuestionsItem() # 实例化Item类 # 获取问题 questions = res.xpath('./div[@class="content-area"]').re(r'?([\s\S]+?)<(table|\/td|div|br)') if len(questions): # 获取题目 question = questions[0].strip() item['source'] = question dr = re.compile(r'<[^>]+>',re.S) question = dr.sub('',question) content = res.extract() item['content'] = question # 获取课目 subject = re.findall(ur'http:\/\/tiku\.xueersi\.com\/shiti\/list_1_(\d+)',response.url) item['subject'] = self.subjects[int(subject[0])-1] # 获取难度等级 levels = res.xpath('//div[@class="info"]').re(ur'难度:([\s\S]+?)<') item['level'] = self.levels.index(levels[0])+1 # 获取选项 options = re.findall(ur'[A-D][\..]([\s\S]+?)<(\/td|\/p|br)',content) item['options'] = options if len(options): url = res.xpath('./div[@class="info"]/a/@href').extract()[0] request = scrapy.Request(url,callback=self.getanswer) request.meta['item'] = item # 缓存item数据,传递给下一个请求 yield request #for option in options: # 获取答案 def getanswer(self,response): res = response.xpath('//div[@class="part"]').re(ur'([\s\S]+?)<\/td>') con = re.findall(ur'([\s\S]+?)
[\s\S]+?([A-D])',res[0]) # 获取含有解析的答案 if con: answer = con[0][1] analysis = con[0][0] # 获取解析 else: answer = res[0] analysis = '' if answer: item = response.meta['item'] # 获取item item['answer'] = answer.strip() item['analysis'] = analysis.strip() item['answer_url'] = response.url yield item # 返回item,输出管道(pipelines.py)会自动接收该数据items.py 数据结构定义:
# -*- coding: utf-8 -*- # Define here the models for your scraped items # # See documentation in: # https://doc.scrapy.org/en/latest/topics/items.html import scrapy class QuestionsItem(scrapy.Item): content = scrapy.Field() subject = scrapy.Field() level = scrapy.Field() answer = scrapy.Field() options = scrapy.Field() analysis = scrapy.Field() source = scrapy.Field() answer_url = scrapy.Field() passpipelines.py 输出管道(本例子输出的数据写入本地数据库):
# -*- coding: utf-8 -*- # Define your item pipelines here # # Don't forget to add your pipeline to the ITEM_PIPELINES setting # See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html import pymysql import md5 class QuestionsPipeline(object): def __init__(self): # 建立数据库连接 self.connect = pymysql.connect('localhost','root','','question',use_unicode=True,charset='utf8') # 获取游标 self.cursor = self.connect.cursor() print("connecting mysql success!") self.answer = ['A','B','C','D'] def process_item(self, item, spider): content = pymysql.escape_string(item['content']) # 获取题目hash值,使用该字段过滤重复的题目 m1 = md5.new() m1.update(content) hash = m1.hexdigest() selectstr = "select id from question where hash='%s'"%(hash) self.cursor.execute(selectstr) res = self.cursor.fetchone() # 过滤相同的题目 if not res: # 插入题目 sqlstr = "insert into question(content,source,subject,level,answer,analysis,hash,answer_url) VALUES('%s','%s','%s','%s','%s','%s','%s','%s')"%(content,pymysql.escape_string(item['source']),item['subject'],item['level'],item['answer'],pymysql.escape_string(item['analysis']),hash,item['answer_url']) self.cursor.execute(sqlstr) qid = self.cursor.lastrowid # 插入选项 for index in range(len(item['options'])): option = item['options'][index] answer = self.answer.index(item['answer']) if answer==index: ans = '2' else: ans = '1' sqlstr = "insert into options(content,qid,answer) VALUES('%s','%s','%s')"%(pymysql.escape_string(option[0]),qid,ans) self.cursor.execute(sqlstr) self.connect.commit() #self.connect.close() return item爬虫构建完毕后,在项目的根目录下运行
scrapy crawl xueersi # scrapy crawl 爬虫的名称上述就是小编为大家分享的如何在python中使用scrapy爬虫库了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联成都网站设计公司行业资讯频道。
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
标题名称:如何在python中使用scrapy爬虫库-创新互联
网址分享:http://scjbc.cn/article/dsigcd.html