ITPub博客

首页 > IT职业 > IT图书 > 爬虫框架的功能组成是什么

爬虫框架的功能组成是什么

IT图书 作者:m53469 时间:2021-09-11 09:52:44 0 删除 编辑

1.调度(scheduler)程序

爬虫框架需要具备哪些功能。Scrapy,pyspider有http请求库,html解析工具,数据库存储等,但其实最核心的是他们的调度(scheduler)程序:即如何让你的请求,解析,存储协同工作。

2.请求,解析,存储

一个最小的爬虫框架只需要一套调度程序就可以了,其他的请求,解析,存储都可以作为框架的扩展来使用,比如:gaoxinge/spidery。另外既然一个最小的爬虫框架只有一套调度程序,那么它也可以用来做非爬虫的工作。

3.实例

# -*- coding: utf-8 -*-
"""
url: https://stackoverflow.com/questions
fetch: requests
parse: lxml
presist: txt
"""
import requests
from lxml import etree
from spidery import Spider
 
spider = Spider(
    urls = ['http://stackoverflow.com/questions/?page=' + str(i) + '&sort=votes' for i in range(1, 4)],
)
 
@spider.fetch
def fetch(url):
    response = requests.get(url)
    return response
 
@spider.parse
def parse(response):
    root = etree.HTML(response.text)
    results = root.xpath('//div[@class='question-summary']')
    for result in results:
        question = {}
        question['votes']   = result.xpath('div[@class='statscontainer']//strong/text()')[0]
        question['answers'] = result.xpath('div[@class='statscontainer']//strong/text()')[1]
        question['views']   = result.xpath('div[@class='statscontainer']/div[@class='views supernova']/text()')[0].strip()
        question['title']   = result.xpath('div[@class='summary']/h3/a/text()')[0]
        question['link']    = result.xpath('div[@class='summary']/h3/a/@href')[0]
        yield question, None
 
@spider.presist
def presist(item):
    f.write(str(item) + 'n')
 
f = open('stackoverflow.txt', 'wb')
spider.consume_all()
f.close()

以上就是爬虫框架的功能组成介绍,在我们对其的一些用法进行了解后,就可以进行展开练习。建议建议结合代理ip的使用,如果大家想测试使用下,可以尝试品易云http代理ip,免费测试包含各种类ip资源,无限调用IP量!更多常见问题解决:ip

推荐操作环境:windows7系统、Python 3.9.1,DELL G3电脑。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2819/viewspace-2829537/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-04-19

  • 博文量
    80
  • 访问量
    13445