因为毕业设计要使用到爬虫技术,因此就去研究了一下,收获了不少东西~
今天要对比的是python三大主流爬虫框架,分别是 requests、scrapy、selenium。
爬虫的原理
网络爬虫的工作原理非常简单,大概可以分为以下四个步骤:
- 请求:爬虫会通过 HTTP 协议向起始 URL 发送请求,并获取响应。在请求中,爬虫需要指定请求方式(如 GET、POST 等)、请求头和请求参数等信息;
- 解析:爬虫会将获取到的响应解析成 HTML 页面,并从页面中提取出需要的数据。通常使用解析库(如 BeautifulSoup)来解析 HTML 页面;
- 存储:爬虫会将提取到的数据存储到本地计算机或数据库中,以备后续分析、处理或展示;
- 遍历:根据解析结果,获取下一步需要访问的网页链接,重复1-3步骤。
爬虫的实现
这次的目标是:批量采集起点中文网的排行榜书籍信息,目标是5个静态页面,每页包含20条信息共100条书籍信息
- requests
from lxml import etree
import time
import requests
# url = f'https://www.qidian.com/rank/yuepiao/year2023-month04-page{}/'
start = time.time() #开始计时
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36'
}
for i in range(1, 11):
print(f'正在爬取第{i}页')
url = f'https://www.qidian.com/rank/yuepiao/year2023-month04-page{i}/'
page = requests.get(url=url, headers=headers)
html = etree.HTML(page.content.decode('utf=8'))
books = html.xpath("//div[@class='book-img-text']/ul/li")
for book in books:
imglink = 'https:' + book.xpath("./div[1]/a/@href")[0]
title = book.xpath("./div[2]/h2/a/text()")[0]
author = book.xpath("./div[2]/p[1]/a[1]/text()")[0]
intro = book.xpath("./div[2]/p[2]/text()")[0]
update = book.xpath("./div[2]/p[3]/a/text()")[0]
print(imglink, title, author, intro, update)
end = time.time() #结束计时
print('耗时:', end - start)
代码运行,时间为 3.4s
- scrapy
先安装 pip install scrapy;
然后新建项目 scrapy startproject '你项目name';
然后进入项目文件夹,继续命令行创建项目:scrapy genspider qidian "qidian.com";
最后在 qidian.py 里面写代码,运行命令是:scrapy crawl qidian;
import scrapy
import time
class QidianSpider(scrapy.Spider):
name = "qidian"
allowed_domains = ["qidian.com"]
# 爬取10页
for i in range(1, 11):
print(f"正在爬取第{i}页")
start_urls = [f"https://www.qidian.com/rank/yuepiao?style=1&page={i}"]
# 去除多余的控制台输出
custom_settings = {
"LOG_LEVEL": "ERROR",
"LOG_FILE": "log.txt",
}
def parse(self, response):
start = time.time() #开始计时
books = response.xpath("//div[@class='book-img-text']/ul/li")
for book in books:
imglink = 'https:' + book.xpath("./div[1]/a/@href").extract()[0]
title = book.xpath("./div[2]/h2/a/text()").extract()[0]
author = book.xpath("./div[2]/p[1]/a[1]/text()").extract()[0]
intro = book.xpath("./div[2]/p[2]/text()").extract()[0]
update = book.xpath("./div[2]/p[3]/a/text()").extract()[0]
print(imglink, title, author, intro, update)
end = time.time() #结束计时
print('耗时:', end - start)
运行代码,时间为 0.04s
- selenium
正常安装并导入即可
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
# url = 'https://www.qidian.com/rank/yuepiao?style=1&page=1'
start = time.time() #开始计时
# 初始化浏览器
driver = webdriver.Chrome()
# 爬取10页
for i in range(1, 11):
print(f'正在爬取第{i}页')
url = f'https://www.qidian.com/rank/yuepiao?style=1&page={i}'
driver.get(url)
books = driver.find_elements(By.XPATH ,"//div[@class='book-img-text']/ul/li")
for book in books:
imglink = 'https:' + book.find_element(By.XPATH ,"./div[1]/a").get_attribute('href')
title = book.find_element(By.XPATH ,"./div[2]/h2/a").text
author = book.find_element(By.XPATH ,"./div[2]/p[1]/a[1]").text
intro = book.find_element(By.XPATH ,"./div[2]/p[2]").text
update = book.find_element(By.XPATH ,"./div[2]/p[3]/a").text
print(imglink, title, author, intro, update)
end = time.time() #结束计时
print('耗时:', end - start)
运行时间为 48s
我们可以发现,
从代码量上来看,其实相差不大,因为实现逻辑比较简单;
从运行时间来看的话:Scrapy 是最快,平均0.05s,而Selenium 是最慢的,运行效率约为 Scrapy 的1/1000。
不过scrapy开发、调试代码的时间相比于 requests、selenium 会长一点。
requests、scrapy 快是快,但是很多网站都设置了反爬,如果遇到反爬,数据动态加载等情况,就会很麻烦,因此,在这种情况下选择 selenium 是很好用的。