準備工作
基礎環境準備
- win10
- pycharm
- python3(同時保證有pip環境)
scrapy環境準備
-
打開網址
https://www.lfd.uci.edu/~gohlke/pythonlibs/
- 下載Twisted(按python版本+系統位數對應)(瀏覽器下ctrl+f直接搜索)
- 下載scrapy (截止本文撰寫,當前版本是:Scrapy-1.6.0-py2.py3-NONE-any.whl)(不區分系統和python版本)
- 準備好這兩個whl文件(我習慣在桌面建個臨時文件夾,然后把他倆扔進去)
- 安裝whl:在文件夾下按住shift 然后點擊右鍵,選擇在此處打開powershell。
-
執行命令
pip install teisted文件名
-
執行命令
pip install scrapy文件名
-
等待安裝完成后 執行命令
pip list
查看已經安裝的python包中是否有上面兩個模塊,正常的話都有了。 - 如圖,安裝成功
pycharm創建一個scrapy項目
創建一個項目
../demodemo/ # 爬蟲項目
demodemo/ # 爬蟲文件夾
__init__.py # 必須存在
items.py # 字段 可以寫需要爬取對象的類
middlewares.py #中間件
pipelines.py # 管道文件
settings.py # 爬取配置
spiders/ # 開發位置
scrapy.cfg # 項目配置
寫一個DEMO
- 在terminal中通過cd命令進入到spiders文件夾
-
執行命令
scrapy genspider 爬蟲名 爬蟲域
- 在我的demo中:``````(對應的是 隨便找了一個段子網站:https://ishuo.cn/ )
- 編輯items.py
# -*- coding: utf-8 -*-
# Define here the models for your scraped items
#
# See documentation in:
# https://doc.scrapy.org/en/latest/topics/items.html
#
# import scrapy
#
#
# class DemodemoItem(scrapy.Item):
# # define the fields for your item here like:
# # name = scrapy.Field()
# pass
import
scrapy
class
DuanZiItem
(
scrapy
.
Item
)
:
# 作者
author
=
scrapy
.
Field
(
)
# 內容
word
=
scrapy
.
Field
(
)
- 編輯spiders/Duanzi.py
# -*- coding: utf-8 -*-
# import scrapy
#
#
# class DuanziSpider(scrapy.Spider):
# name = 'Duanzi'
# allowed_domains = ['ishuo.cn']
# start_urls = ['http://ishuo.cn/']
#
# def parse(self, response):
# pass
# -*- coding: utf-8 -*-
import
scrapy
from
demodemo
.
items
import
DuanZiItem
class
DemospiderSpider
(
scrapy
.
Spider
)
:
# 爬蟲名稱
name
=
'duanzi'
# 爬蟲域
allowed_domains
=
[
'ishuo.cn'
]
# 爬蟲地址
start_urls
=
[
'http://ishuo.cn/'
]
# 處理回復
def
parse
(
self
,
response
)
:
data_list
=
[
]
text_list
=
response
.
xpath
(
"http://div[@id=\"list\"]/ul/li"
)
for
item
in
text_list
:
word
=
item
.
xpath
(
'./div[@class=\"content\"]/text()'
)
.
extract
(
)
[
0
]
author
=
item
.
xpath
(
'./div[@class="info"]/a/text()'
)
.
extract
(
)
[
0
]
ddata
=
DuanZiItem
(
)
ddata
[
'author'
]
=
author
ddata
[
'word'
]
=
word
data_list
.
append
(
ddata
)
pass
# print(data_list)
with
open
(
"demo.txt"
,
"w"
,
encoding
=
"utf-8"
)
as
f
:
f
.
write
(
str
(
data_list
)
)
-
編輯setting.py
修改一個值
ROBOTSTXT_OBEY
=
False
添加一個請求頭
DEFAULT_REQUEST_HEADERS
=
{
"user-agent"
:
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"
,
'Accept'
:
'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
,
# 'Accept-Language': 'en',
}
-
注意:這句話會報錯
from demodemo.items import DuanZiItem
是因為本地包路徑的問題
解決方案:選中項目名
(是scrapy項目名:demodemo 不是pycharm項目名demo)
右鍵
運行scrapy
執行命令:
scrapy crawl duanzi
即可看到一個txt文件
如何讓pycharm運行scrapy
-
編寫一個腳本
放到項目根目錄(與.cfg同目錄)
start.py
# -*- coding:utf-8 -*-
from
scrapy
import
cmdline
cmdline
.
execute
(
"scrapy crawl duanzi"
.
split
(
)
)
3.
4.選擇剛才的start.py 并給這個腳本起個名字
apply
【完】
歡迎關注我的微信公眾號:并非一無所有
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
