Twisted:爲 Python 提供的基於事件驅動的網絡引擎包。html
在下面網址安裝Twistedpython
cmd輸入pip install scrapy
web
安裝完畢,cmd裏輸入scrapy出現安裝成功。chrome
引擎,用來處理整個系統的數據流處理,觸發事務。shell
調度器,用來接受引擎發過來的請求,壓入隊列中,並在引擎再次請求的時候返回。json
下載器,用於下載網頁內容,並將網頁內容返回給蜘蛛。api
蜘蛛,蜘蛛是主要幹活的,用它來制訂特定域名或網頁的解析規則。瀏覽器
項目管道,負責處理有蜘蛛從網頁中抽取的項目,主要任務是清晰、驗證和存儲數據。當頁面被蜘蛛解析後,將被髮送到項目管道,並通過幾個特定的次序處理數據。bash
下載器中間件,位於Scrapy引擎和下載器之間的鉤子框架,主要是處理Scrapy引擎與下載器之間的請求及響應。網絡
蜘蛛中間件,介於Scrapy引擎和蜘蛛之間的鉤子框架,主要工做是處理蜘蛛的響應輸入和請求輸出。
調度中間件,介於Scrapy引擎和調度之間的中間件,從Scrapy引擎發送到調度的請求和響應。
引擎打開一個域名時,蜘蛛處理這個域名,並讓蜘蛛獲取第一個爬取的URL。
引擎從蜘蛛那獲取第一個須要爬取的URL,而後做爲請求在調度中進行調度。
引擎從調度那獲取接下來進行爬取的頁面。
調度將下一個爬取的URL返回給引擎,引擎將他們經過下載中間件發送到下載器。
當網頁被下載器下載完成之後,響應內容經過下載中間件被髮送到引擎。
引擎收到下載器的響應並將它經過蜘蛛中間件發送到蜘蛛進行處理。
蜘蛛處理響應並返回爬取到的項目,而後給引擎發送新的請求。
引擎將抓取到的項目項目管道,並向調度發送請求。
系統重複第二部後面的操做,直到調度中沒有請求。
新建文件夾命令爲天氣網爬蟲
cd到根目錄,打開cmd,運行scrapy startproject weather_spider
建立spider
scrapy genspider weather www.aqistudy.cn/historydata
這裏的weather是spider的名字
建立的路徑以下:
item.py
對於爬取必須假裝好UA,在setting.py中定義MY_USER_AGENT
來存放UA,注意在settings中命名必須大寫
settings.py
MY_USER_AGENT = [
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; AcooBrowser; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Acoo Browser; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506)",
"Mozilla/4.0 (compatible; MSIE 7.0; AOL 9.5; AOLBuild 4337.35; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
"Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US)",
"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 2.0.50727; Media Center PC 6.0)",
"Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 1.0.3705; .NET CLR 1.1.4322)",
"Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.04506.30)",
"Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN) AppleWebKit/523.15 (KHTML, like Gecko, Safari/419.3) Arora/0.3 (Change: 287 c9dfb30)",
"Mozilla/5.0 (X11; U; Linux; en-US) AppleWebKit/527+ (KHTML, like Gecko, Safari/419.3) Arora/0.6",
"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2pre) Gecko/20070215 K-Ninja/2.1.1",
"Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/20080705 Firefox/3.0 Kapiko/3.0",
"Mozilla/5.0 (X11; Linux i686; U;) Gecko/20070322 Kazehakase/0.4.5",
"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.8) Gecko Fedora/1.9.0.8-1.fc10 Kazehakase/0.5.6",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.20 (KHTML, like Gecko) Chrome/19.0.1036.7 Safari/535.20",
"Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; fr) Presto/2.9.168 Version/11.52",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.11 TaoBrowser/2.0 Safari/536.11",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.71 Safari/537.1 LBBROWSER",
"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; LBBROWSER)",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E; LBBROWSER)",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.84 Safari/535.11 LBBROWSER",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)",
"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; QQBrowser/7.0.3698.400)",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E)",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; SV1; QQDownload 732; .NET4.0C; .NET4.0E; 360SE)",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E)",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)",
"Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1",
"Mozilla/5.0 (iPad; U; CPU OS 4_2_1 like Mac OS X; zh-cn) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148 Safari/6533.18.5",
"Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:2.0b13pre) Gecko/20110307 Firefox/4.0b13pre",
"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:16.0) Gecko/20100101 Firefox/16.0",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11",
"Mozilla/5.0 (X11; U; Linux x86_64; zh-CN; rv:1.9.2.10) Gecko/20100922 Ubuntu/10.10 (maverick) Firefox/3.6.10",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36",]複製代碼
RandomUserAgentMiddleware
類
middlewares.py
注意要在settings.py中激活,必須是900,來去掉scrapy自己的UA
setting.py
開始編寫最重要的spider.py,推薦使用scrapy.shell來一步一步調試
先拿到全部的城市
weather.py
def parse(self, response): city_urls = response.xpath('//div[@class="all"]/div[@class="bottom"]//li/a/@href').extract()[16:17] city_names = response.xpath('//div[@class="all"]/div[@class="bottom"]//li/a/text()').extract()[16:17] self.logger.info('正在爬去{}城市url'.format(city_names[0])) for city_url, city_name in zip(city_urls, city_names): # 用的follow快捷方式,能夠自動拼接url yield response.follow(url=city_url, meta={'city': city_name}, callback=self.parse_month)複製代碼
weather.py
def parse_month(self, response): """ 解析月份的url :param response: :return: """ city_name = response.meta['city'] self.logger.info('正在爬取{}城市的月份url'.format(city_name[0])) # 因爲爬取的信息太大了,全部先爬取前5個 month_urls = response.xpath('//ul[@class="unstyled1"]/li/a/@href').extract()[0:5] for month_url in month_urls: yield response.follow(url=month_url, meta={'city': city_name, 'selenium': True}, callback=self.parse_day_data)複製代碼
WeatherSpiderDownloaderMiddleware
下載中間件中的process_request函數方法
middlewares.py
import time
import scrapy
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
class WeatherSpiderDownloaderMiddleware(object):
def process_request(self, request, spider):
if request.meta.get('selenium'):
# 爲了讓瀏覽器可以無界面的工做
chrome_options = Options()
# 設置chrome瀏覽器無界面模式
chrome_options.add_argument('--headless')
driver = webdriver.Chrome(chrome_options=chrome_options)
# 用瀏覽器去訪問這個地址
driver.get(request.url)
time.sleep(1.5) # 由於瀏覽器須要加載渲染
html = driver.page_source
driver.quit()
return scrapy.http.HtmlResponse(url=request.url, body=html, encoding='utf-8', request=request)
return None複製代碼
激活WeatherSpiderDownloaderMiddleware
最後編寫weather.py中的剩下代碼
scrapy list
查看是否存在項目
scrapy crawl weather -o spider.json
scrapy crawl weather -o spider..jl
scrapy crawl weather -o spider..csv
scrapy crawl weather -o spider..xml
FEED_EXPORT_ENCODING = 'utf-8'
對於入門主要處理的是pipelines中
pipelines.py
在settings中激活pipelines
效果以下