当前位置:网站首页>站点数据收集-Scrapy使用笔记
站点数据收集-Scrapy使用笔记
2022-07-29 10:25:00 【c01dkit】
前言
网站数据收集方法有很多,比如最基础的requests,简单几行就可以获取网页信息。使用selenium模拟网页点击可以绕过很多反爬策略,编写思路也不同于其他的方法。用scrapy框架来做的话可以清楚地进行目标拆分,并利用内置的线程池可以非常高效地获取信息。
本文以scrapy为目标,总结基础的使用方法,以供后续复习。
配置
本地配置好python及pip后,使用pip install scrapy
既可以安装scrapy。
基本使用
新建工程
scrapy在使用时,需要在主机命令行里scrapy startproject <projectname>
创建一个项目,比如运行scrapy startproject example
后生成example文件夹,内容如图所示。
添加目标网站
命令行也会提示进入example目录,并运行scrapy genspider来创建一个spider。比如运行scrapy genspider example_spider example.com
,之后会在spiders文件夹下生成一个example_spider.py文件。爬虫的代码就需要写在这个文件内。
GET请求
import scrapy
class ExampleSpiderSpider(scrapy.Spider):
name = 'example_spider'
allowed_domains = ['example.com']
start_urls = ['http://example.com/']
def parse(self, response):
pass
POST请求
import scrapy
class ExampleSpiderSpider(scrapy.Spider):
name = 'example_spider'
allowed_domains = ['example.com']
urls = [
'https://example.com/page/1/',
'https://example.com/page/2/',
]
def start_requests(self):
for target in urls:
#发送'Content-Type':'application/x-www-form-urlencoded'的请求
yield scrapy.FormRequest(
url=url,
formdata={
'arg1':'xxx','arg2':'xxx'},
callback=self.parse,
meta={
'arg1':1,'arg2':2}
)
#发送'Content-Type':'application/json'的请求
yield scrapy.Request(
url=url,
method='POST',
body = json.dumps({
'arg1':'xxx','arg2':'xxx'}),
headers = {
'Content-Type':'application/json'},
callback=self.parse,
meta={
'arg1':1,'arg2':2}
)
def parse(self, response):
pass
需要注意的是:
name
是爬虫名字,即spidername,之后运行需要指定这个名字。allowed_domains
指定允许爬取的域名,也可以不要。start_urls
指定需要爬取哪些网站,运行时会一个一个向这些网站发请求,并将响应传给parse函数。如果需要动态生成目标网站,可以删掉这个start_urls
变量,并添加一个start_requests(self)
成员函数(需要使用yield scrapy.Request(url = <targetwebsite>, callback=self.parse)
作为返回值。爬虫运行时如果发现没有定义start_urls
变量,则会调用这个函数。scrapy.Request
用于发送GET请求。可以添加一个cb_kwargs
参数,它接受一个字典,并可以在parse(self, response, **kwargs)
中通过kwargs
来获取这个字典,以实现自定义的参数传递。也可以使用meta
参数,然后在parse里用response.meta
获取传递的字典。scrapy.FormRequest
用于发送POST请求,请求体放在formdata
中,参数应当都是字符串类型。可以使用meta
来进行参数传递(也可以用cb_kwargs? 没有测试)
这里对官方文档作以修改举例:
import scrapy
class QuotesSpider(scrapy.Spider):
name = "quotes"
def start_requests(self):
urls = [
'https://quotes.toscrape.com/page/1/',
'https://quotes.toscrape.com/page/2/',
]
for url in urls:
yield scrapy.Request(url=url, callback=self.parse, cb_kwargs={
'this_url':url})
def parse(self, response, **kwargs):
page = response.url.split("/")[-2]
url = kwargs['this_url']
filename = f'quotes-{
page}.html'
with open(filename, 'wb') as f:
f.write(response.body)
self.log(f'Saved file {
filename}')
启动爬取
在最外层的example目录下运行scrapy crawl <spidername>
,即可开始爬取。
再以官网文档为例:
import scrapy
class QuotesSpider(scrapy.Spider):
name = "quotes"
start_urls = [
'https://quotes.toscrape.com/page/1/',
'https://quotes.toscrape.com/page/2/',
]
def parse(self, response):
for quote in response.css('div.quote'):
yield {
'text': quote.css('span.text::text').get(),
'author': quote.css('small.author::text').get(),
'tags': quote.css('div.tags a.tag::text').getall(),
}
response.css来提取元素,其含义不言自明。也可以通过response.text获取文本信息。
这里的parse函数yield了一个字典,可以在运行时指定保存文件:scrapy crawl <spidername> -O <output.jl>
来将其保存到文件中,方便后续处理。jl是jsonline即单行json,可以在python中使用简单的文件逐行遍历配合json来处理。其中-O
表示覆盖输出文件,-o
表示在输出文件后追加。可以添加-L ERROR
来忽略运行时无关紧要的输出。
对公开API继续爬取时,jl有奇效。
边栏推荐
- Tell you from my accident: Mastering asynchrony is key
- ECCV 2022 | CMU proposes to recurse on the visual transformer without adding parameters, and the amount of calculation is still small
- VMWare:使用命令更新或升级 VMWare ESXi 主机
- [HFCTF 2021 Final]easyflask
- PDF处理还收费?不可能
- Print out the "hourglass" and the remaining number according to the given number of characters and characters
- 【论文阅读】Q-BERT: Hessian Based Ultra Low Precision Quantization of BERT
- Drunken driving alarm system based on stm32
- Understanding of Arduino circuit
- TMS320C6000_ Tms320f28035 Chinese data manual
猜你喜欢
Hanyuan high tech Gigabit 2-optical 6-conductor rail managed Industrial Ethernet switch supports X-ring redundant ring network one key ring network switch
Easy to understand and explain the gradient descent method!
factoextra:多元统计方法的可视化PCA
How beautiful can VIM be configured?
After eating Alibaba's core notes of highly concurrent programming, the backhand rose 5K
【论文阅读】Q-BERT: Hessian Based Ultra Low Precision Quantization of BERT
12代酷睿处理器+2.8K OLED华硕好屏,灵耀14 2022影青釉商务轻薄本
这才是开发者神器正确的打开方式
Vim到底可以配置得多漂亮?
数据可视化设计指南(信息图表篇)
随机推荐
Tell you from my accident: Mastering asynchrony is key
Introduction to distributed scheduling xxl-job features
NUMA architecture CPU API change summary
2022cuda summer training camp Day2 practice
The server
若依如何实现添加水印功能
为什么要使用markdown进行写作?
二次握手??三次挥手??
Enterprise architecture | togaf architecture capability framework
【日志框架】
98. (cesium chapter) cesium point heat
Consumer electronics, frozen to death in summer
Correct posture and landing practice of R & D efficiency measurement (speech ppt sharing version)
Implementation of college logistics repair application system based on SSM
ADDS:使用 PowerShell 创建 OU 结构
Where are those test / development programmers in their 30s? a man should be independent at the age of thirty......
ggdag 绘制DAG和因果图
How big is the bandwidth of the Tiktok server for hundreds of millions of people to brush at the same time?
这才是开发者神器正确的打开方式
Performance optimization analysis tool | perf