BeautifulSoup/Scrapy/Selenium爬虫框架的不同和使用方法
2020-06-05 08:34
267 查看
记录一下几个爬虫框架的区别和使用方法:
Request & BeautifulSoup
最简单基础
使用方法
import requests from bs4 import BeautifulSoup for page in range(20): url = 'https://www.网址.com/'.format(page) req = requests.get(url) html = req.text soup = BeautifulSoup(html, 'lxml')
然后写一个for循环,用.select()或.find_all()实现CSS选择器,例如:
for article in soup.select('.article'): joke = article.select('a.contentHerf .content span')[0].string#从列表中第一个元素提取字符串 likes = article.select('.stats .stats-vote .number')[0].string if joke is not None and int(likes)>1000: print(joke, likes)
Scrapy
适用于静态刷新网站,如网易新闻。已经给出简单的框架结构,方便使用
官方文档
https://docs.scrapy.org/en/latest/
项目文件
引擎(Scrapy Engine)
调度器(Scheduler)
下载器(Downloader)
蜘蛛(Spiders)
项目管道(Item Pipeline)
使用方法
- 进入要存储代码的目录 (命令行下) 然后输入
scrapy startproject tutorial
- 在项目根目录运行
scrapy genspider 项目名 网址
例如:scrapy genspider quotes quotes.toscrape.com
然后会在spider文件夹中自动生成如下文件 - 可以在各个文件中增添自己需要做的操作(注意:不同spider的name不能相同)
spider里需要import的基本内容
from scrapy.linkextractors import LinkExtractor from scrapy.spiders import CrawlSpider,Rule
- 运行:进入 根目录输入
scrapy crawl quotes
如果使用pycharm运行,可以在根目录新建一个py文件,输入下面代码后运行
from scrapy import cmdline cmdline.execute('scrapy crawl 项目名'.split())
Selenium
适用于动态刷新网站,比如淘宝。但爬取速度较慢,配置有点麻烦
配置
Chrome浏览器
chromedriver.exe
pip install selenium
pip install pyquery
相关文章推荐
- 讲解Python的Scrapy爬虫框架使用代理进行采集的方法
- 讲解Python的Scrapy爬虫框架使用代理进行采集的方法
- python爬虫开发之Beautiful Soup模块从安装到详细使用方法与实例
- 零基础写python爬虫之使用Scrapy框架编写爬虫
- Python爬虫是scrapy框架中ItemLoaders使用解析
- 使用python scrapy爬虫框架 爬取科学网自然科学基金数据
- Python爬虫知识(1)——scrapy vs requests+BeautifulSoup
- 萌新的Python学习日记 - 爬虫无影 - 使用BeautifulSoup + css selector 抓取自己想要网页内容
- 【Scrapy框架中使用selenium获取动态数据案例】
- 【python爬虫】scrapy框架笔记(一):创建工程,使用scrapy shell,xpath
- selenium+BeautifulSoup实现强大的爬虫功能
- 基于Python使用scrapy-redis框架实现分布式爬虫 注
- Python爬虫scrapy框架爬取动态网站——scrapy与selenium结合爬取数据
- python爬虫框架之Scrapy之分布式 + selenium爬取蘑菇街
- Python 爬虫框架 Scrapy 快速使用
- windows下使用python的scrapy爬虫框架,爬取个人博客文章内容信息
- 零基础写python爬虫之使用Scrapy框架编写爬虫
- 解决Struts1.x中使用Validator框架对DispatchAction不同方法使用不同的字段验证
- Scrapy爬虫框架使用IP代理池
- 【python爬虫03】使用Scrapy框架模拟登录知乎