scrapy教程 安装使用
Last updated on November 22, 2024 pm
🧙 Questions
官方快速入门
☄️ Ideas
环境
- python
- pip
1. 安装scrapy
python -m pip install --upgrade pip
pip install scrapy
2. 初始化项目
Note: 项目名称不可以包含中划线
scrapy startproject leo_day_scrapy
# 创建新的爬虫
scrapy genspider example example.com
3. 目录结构说明
tutorial/
scrapy.cfg # 部署配置文件
tutorial/ # 项目模块
__init__.py # 入口
items.py # project items definition file
middlewares.py # project middlewares file
pipelines.py # project pipelines file
settings.py # project settings file
spiders/ # a directory where you'll later put your spiders
__init__.py # spiders中的入口文件
4. 官方demo
import scrapy
class QuotesSpider(scrapy.Spider):
name = "quotes"
def start_requests(self):
urls = [
'http://quotes.toscrape.com/page/1/',
'http://quotes.toscrape.com/page/2/',
]
for url in urls:
yield scrapy.Request(url=url, callback=self.parse)
def parse(self, response):
page = response.url.split("/")[-2]
filename = f'quotes-{page}.html'
with open(filename, 'wb') as f:
f.write(response.body)
self.log(f'Saved file {filename}')
🔗 Links
pip install mysql-connector-python
scrapy教程 安装使用
https://ispong.isxcode.com/pytorch/scrapy/scrapy教程 安装使用/