1. 程式人生 > >Scrapy爬取簡單百度頁面

Scrapy爬取簡單百度頁面

Scrapy爬取百度頁面

------------------------------------------

spiders-baiduspider.py

 

 1 '''
 2 要求匯入scrapy
 3 所有類一般是XXXSpider命名
 4 所有爬蟲類是scrapy.Spider的子類
 5 scrapy爬取百度
 6 關閉配置的機器人協議
 7 '''
 8 
 9 import scrapy
10 
11 class BaiduSpider(scrapy.Spider):
12 
13     # name是爬蟲的名稱
14     name = "baidu
" 15 16 # 起始url列表 17 start_urls = ['http://www.baidu.com'] 18 19 20 # 負責分析downloader下載得到的結果 21 def parse(self, response): 22 ''' 23 只是儲存網頁即可 24 :param response: 25 :return: 26 ''' 27 with open('baidu.html', 'w', encoding='utf-8') as f:
28 f.write(response.body.decode('utf-8'))

 

 

 

 

 

 

 ===========================

 

start_urls = xxxxxxxxxxxxxxxxxxxx  起始地址

parse函式分析網頁:網頁已經被downloader下來了,重寫spider的parse函式

scrapy crawl baidu 終端下執行(name = "baidu")