剛接觸python爬蟲不久,想對學習的spider機制作一個簡單的概括。html
爬蟲,即網絡爬蟲,你們能夠理解爲在網絡上爬行的一直蜘蛛,互聯網就比做一張大網,而爬蟲即是在這張網上爬來爬去的蜘蛛咯,若是它遇到資源,那麼它就會抓取下來。想抓取什麼?這個由你來控制它咯。python
好比它在抓取一個網頁,在這個網中他發現了一條道路,其實就是指向網頁的超連接,那麼它就能夠爬到另外一張網上來獲取數據。這樣,整個連在一塊兒的大網對這隻蜘蛛來講觸手可及,想抓取什麼就能夠隨這隻蜘蛛的想法而決定了。正則表達式
爬蟲的基本流程:數據庫
抓取網絡目標,用戶獲取網絡數據的方式有三種:瀏覽器
方式一:瀏覽器提交請求-----> 下載網頁代碼 ----> 解析頁面cookie
#!/usr/bin/env python2 # -*- coding: utf-8 -*- # author:Momo time:2018/6/28 import urllib2 url = "http://baike.baidu.com/item/Python" # 第一種方法:最簡潔的方法 print '第一種方法' response1 = urllib2.urlopen(url) # 直接請求 print response1.getcode() # 獲取狀態碼,若是是200表示獲取成功 print len(response1.read()) # 讀取內容response.read
這種方法最簡單網絡
方式二: 模擬瀏覽器發生請求(獲取網頁代碼)-----> 提取有用的數據 ------> 存放於數據庫或者文件中python爬蟲
#!/usr/bin/env python2 # -*- coding: utf-8 -*- # author:Momo time:2018/6/28 import urllib2 # 第二種方法:添加data、http header print '第二種方法' request = urllib2.Request(url) # 建立request對象 # 添加數據:request.add_data('a','1') request.add_header("user-agent", "Mozill/5.0") # 添加http的header response2 = urllib2.urlopen(request) # 發送請求結果 print response2.getcode() print len(response2.read())
第三種方法:添加特殊場景的處理器
#!/usr/bin/env python # -*- coding: utf-8 -*- # author:Momo time:2018/6/28 print'第三章方法' cj = cookielib.CookieJar() # 建立cookie容器 opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) # 建立一個opener urllib2.install_opener(opener) # 給urllib2安裝opener response3 = urllib2.urlopen(url) # 使用帶有cookie的urllib2訪問網頁 print response3.getcode() print cj print response3.read()
網頁搜索策略ide
正則表達式或者xpath,我的以爲xpath比較好用。學習
下面是一段我在百度貼吧裏用正則表達式抓取圖片的代碼。
1 #!/usr/bin/env python2 2 # -*- coding: utf-8 -*- 3 # author:Momo time:2018/6/28 4 5 6 import urllib 7 import re 8 9 def get_html(url): 10 page = urllib.urlopen(url) 11 html_code = page.read() 12 return html_code 13 14 def get_image(html_code): 15 reg = r'src="(.+?\.jpg)" width' 16 reg_img = re.compile(reg) 17 img_list = reg_img.findall(html_code) 18 x = 0 19 for img in img_list: 20 urllib.urlretrieve(img, '%s.jpg' % x) 21 x += 1 22 23 print u'-------網頁圖片抓取-------' 24 print u'請輸入url:', 25 url = raw_input() 26 print url 27 if url: 28 pass 29 else: 30 print u'---沒有地址輸入正在使用默認地址---' 31 url = 'http://tieba.baidu.com/p/1753935195' 32 print u'----------正在獲取網頁---------' 33 html_code = get_html(url) 34 print u'----------正在下載圖片---------' 35 get_image(html_code) 36 print u'-----------下載成功-----------' 37 raw_input('Press Enter to exit')
這是用python3 寫的相同功能的代碼:
1 #!/usr/bin/env python3 2 # -*- coding: utf-8 -*- 3 # author:Momo time:2018/6/29 4 5 import urllib.request 6 import urllib 7 import re 8 9 # url = "http://tieba.baidu.com/p/1753935195" 10 11 def get_html(url): 12 response = urllib.request.urlopen(url) 13 html_code = response.read().decode('utf-8') 14 return html_code 15 16 def get_img(html_code): 17 reg = r'src="(.+?\.jpg)" width' 18 reg_img = re.compile(reg) 19 imglist = reg_img.findall(html_code) 20 x =0 21 for img in imglist: 22 urllib.request.urlretrieve(img, "tieba%s.jpg" % x) 23 x += 1 24 25 print("請用戶輸入url:") 26 url = input() 27 if url != "": 28 pass 29 else: 30 print("----------用戶沒有輸入url,正在使用默認地址---------") 31 url = "http://tieba.baidu.com/p/1753935195" 32 print("---------正在獲取網頁信息-------------") 33 html_code = get_html(url) 34 print("---------正在下載網頁圖片-------------") 35 get_img(html_code)
這裏須要注意的是,python2 和 python3 在網頁獲取和讀取網頁代碼這塊有點區別
python2
import urllib def get_html(url): page = urllib.urlopen(url) html_code = page.read() return html_code
python3
import urllib.request import urllib def get_html(url): response = urllib.request.urlopen(url) html_code = response.read().decode('utf-8') return html_code