温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python爬虫框架的组成需要具备什么功能

发布时间:2021-05-12 12:28:37 来源:亿速云 阅读:153 作者:小新 栏目:编程语言

这篇文章主要介绍Python爬虫框架的组成需要具备什么功能,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

1、组成

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

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

2、实例

# -*- 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\']/h4/a/text()')[0]
        question['link']    = result.xpath('div[@class=\'summary\']/h4/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()

python的数据类型有哪些?

python的数据类型:1. 数字类型,包括int(整型)、long(长整型)和float(浮点型)。2.字符串,分别是str类型和unicode类型。3.布尔型,Python布尔类型也是用于逻辑运算,有两个值:True(真)和False(假)。4.列表,列表是Python中使用最频繁的数据类型,集合中可以放任何数据类型。5. 元组,元组用”()”标识,内部元素用逗号隔开。6. 字典,字典是一种键值对的集合。7. 集合,集合是一个无序的、不重复的数据组合。

以上是“Python爬虫框架的组成需要具备什么功能”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI