
本文共 3893 字,大约阅读时间需要 12 分钟。
Scrapy���������������������������������������������������������������������������������������������Scrapy������������������������������������������������������������������������������
1. Scrapy���������������������������
���������Scrapy���������������������������������������������������
������1������������������������Spider������ Scrapy������������������������������������������������������������������scrapyproj
������������������������������������������������������������������������������������
scrapy startproject demo
������2���������Spider������������������ ������������������������������������������������������������������������
scrapy genspider demo_spider
������������������������������������������������������������demo_spider.py
������������������������������
������3���������Item Pipeline Item Pipeline���������������������������������������������������������������������������������Item Pipeline���������
- CSV������
- JSON������
- ���������������������MySQL���MongoDB������
������4��������������������� Scrapy���������������������������������������������scrapy.cfg
������������������settings.py
���������������������������������������������������
- ���������������������������������������
CONCURRENT_REQUESTS
��� - ���������������������
DOWNLOAD_DELAY
��� - ���������������User-Agent���
DEFAULT_REQUEST_HEADERS
���
2. Scrapy���������������������
���Scrapy������������������������������������������������������
2.1 Request��� Request
���������������HTTP���������������Spider������������Downloader���������������������������������������������������������������
.url
: Request���������URL������.method
: ������������������GET
���POST
.headers
: ������������������������.body
: ������������.meta
: ������������������������
2.2 Response��� Response
������������������HTTP������������Downloader������������Spider���������������������������������������
.url
: ���������URL������.status
: HTTP������������������������200.headers
: ������������������.body
: ������������.request
: ������������Request
������
2.3 Item��� Item
������������������������������������������������������������������������������������������������������������������������������
item = { 'name': '������', 'age': 30, 'hobbies': ['������', '������']}
3. Scrapy���������������������������
Scrapy������������HTML���������������������
- BeautifulSoup
- lxml
- re���������������������
- XPath Selector���������XML���������������������������
- CSS Selector���������CSS������������
���������������CSS Selector������������
response.css('a::attr(href)').extract()
4. ������������
���������������������������������������scrapy crawl demo
���������������������������������������������������������
���������������������
File "e:\program files (x86)\python\lib\site-packages\scrapy\extensions\telnet.py", line 12, infrom twisted.conch import manhole, telnetFile "e:\program files (x86)\python\lib\site-packages\twisted\conch\manhole.py", line 241 def addOutput(self, data, async=False): ^SyntaxError: invalid syntax
��������������� ������������������������Twisted���������������������������������������������manhole.py
���������������������������������������������Python������������������������������������������
���������������
pip install -i https://pypi.org/simple twisted
scrapy upgrade
���������������������Python���������������������������
python -m pip install --upgrade
��������� Scrapy������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������Python������������������������������������������������������������������������������������������������������
发表评论
最新留言
关于作者
