爬蟲(chóng)簡(jiǎn)介
網(wǎng)絡(luò)爬蟲(chóng)(又被稱(chēng)為網(wǎng)頁(yè)蜘蛛,網(wǎng)絡(luò)機(jī)器人,在FOAF社區(qū)中間,更經(jīng)常的稱(chēng)為網(wǎng)頁(yè)追逐者),是一種按照一定的規(guī)則,自動(dòng)地抓取萬(wàn)維網(wǎng)信息的程序或者腳本。另外一些不常使用的名字還有螞蟻、自動(dòng)索引、模擬程序或者蠕蟲(chóng)。
其實(shí),說(shuō)白了就是爬蟲(chóng)可以模擬瀏覽器的行為做你想做的事,訂制化自己搜索和下載的內(nèi)容,并實(shí)現(xiàn)自動(dòng)化的操作。比如瀏覽器可以下載小說(shuō),但是有時(shí)候并不能批量下載,那么爬蟲(chóng)的功能就有用武之地了。
實(shí)現(xiàn)爬蟲(chóng)技術(shù)的編程環(huán)境有很多種,Java,Python,C++等都可以用來(lái)爬蟲(chóng)。但是博主選擇了Python,相信很多人也一樣選擇Python,因?yàn)镻ython確實(shí)很適合做爬蟲(chóng),豐富的第三方庫(kù)十分強(qiáng)大,簡(jiǎn)單幾行代碼便可實(shí)現(xiàn)你想要的功能,更重要的,Python也是數(shù)據(jù)挖掘和分析的好能手。
requests庫(kù)
Requests 唯一的一個(gè)非轉(zhuǎn)基因的 Python HTTP 庫(kù),人類(lèi)可以安全享用。
警告:非專(zhuān)業(yè)使用其他 HTTP 庫(kù)會(huì)導(dǎo)致危險(xiǎn)的副作用,包括:安全缺陷癥、冗余代碼癥、重新發(fā)明輪子癥、啃文檔癥、抑郁、頭疼、甚至死亡。
Requests 允許你發(fā)送純天然,植物飼養(yǎng)的 HTTP/1.1 請(qǐng)求,無(wú)需手工勞動(dòng)。你不需要手動(dòng)為 URL 添加查詢(xún)字串,也不需要對(duì) POST 數(shù)據(jù)進(jìn)行表單編碼。Keep-alive 和 HTTP 連接池的功能是 100% 自動(dòng)化的,一切動(dòng)力都來(lái)自于根植在 Requests 內(nèi)部的 urllib3。
各種請(qǐng)求方式
requests里提供個(gè)各種請(qǐng)求方式
import requests
requests.post("http://httpbin.org/post")
requests.put("http://httpbin.org/put")
requests.delete("http://httpbin.org/delete")
requests.head("http://httpbin.org/get")
requests.options("http://httpbin.org/get")
基本GET請(qǐng)求
import requests
response = requests.get('http://httpbin.org/get')
print(response.text)
帶參數(shù)的GET請(qǐng)求
import requests
response = requests.get("http://httpbin.org/get?name=zhaofan&age=23")
print(response.text)
import requests
data = {
"name":"zhaofan",
"age":22
}
response = requests.get("http://httpbin.org/get",params=data)
print(response.url)
print(response.text)
上述兩種的結(jié)果是相同的,通過(guò)params參數(shù)傳遞一個(gè)字典內(nèi)容,從而直接構(gòu)造url
注意:第二種方式通過(guò)字典的方式的時(shí)候,如果字典中的參數(shù)為None則不會(huì)添加到url上
基本POST請(qǐng)求
通過(guò)在發(fā)送post請(qǐng)求時(shí)添加一個(gè)data參數(shù),這個(gè)data參數(shù)可以通過(guò)字典構(gòu)造成,這樣
對(duì)于發(fā)送post請(qǐng)求就非常方便
import requests
data = {
"name":"zhaofan",
"age":23
}
response = requests.post("http://httpbin.org/post",data=data)
print(response.text)
同樣的在發(fā)送post請(qǐng)求的時(shí)候也可以和發(fā)送get請(qǐng)求一樣通過(guò)headers參數(shù)傳遞一個(gè)字典類(lèi)型的數(shù)據(jù)
響應(yīng)
我們可以通過(guò)response獲得很多屬性,例子如下
import requests
response = requests.get("http://www.baidu.com")
print(type(response.status_code),response.status_code)
print(type(response.headers),response.headers)
print(type(response.cookies),response.cookies)
print(type(response.url),response.url)
print(type(response.history),response.history)
獲取cookie
import requests
response = requests.get("http://www.baidu.com")
print(response.cookies)
for key,value in response.cookies.items():
print(key+"="+value)
會(huì)話(huà)維持
cookie的一個(gè)作用就是可以用于模擬登陸,做會(huì)話(huà)維持
import requests
s = requests.Session()
s.get("http://httpbin.org/cookies/set/number/123456")
response = s.get("http://httpbin.org/cookies")
print(response.text)
證書(shū)驗(yàn)證
現(xiàn)在的很多網(wǎng)站都是https的方式訪問(wèn),所以這個(gè)時(shí)候就涉及到證書(shū)的問(wèn)題
import requests
response = requests.get("https:/www.12306.cn")
print(response.status_code)
import requests
from requests.packages import urllib3
urllib3.disable_warnings()
response = requests.get("https://www.12306.cn",verify=False)
print(response.status_code)
代理設(shè)置
import requests
proxies= {
"http":"http://127.0.0.1:9999",
"https":"http://127.0.0.1:8888"
}
response = requests.get("https://www.baidu.com",proxies=proxies)
print(response.text)
超時(shí)設(shè)置
通過(guò)timeout參數(shù)可以設(shè)置超時(shí)的時(shí)間
認(rèn)證設(shè)置
如果碰到需要認(rèn)證的網(wǎng)站可以通過(guò)requests.auth模塊實(shí)現(xiàn)
import requests
from requests.auth import HTTPBasicAuth
response = requests.get("http://120.27.34.24:9001/",auth=HTTPBasicAuth("user","123"))
print(response.status_code)
import requests
response = requests.get("http://120.27.34.24:9001/",auth=("user","123"))
print(response.status_code)
xpath解析Dom樹(shù)
XPath 是一門(mén)在 XML 文檔中查找信息的語(yǔ)言。
XPath 是 XSLT 中的主要元素。
XQuery 和 XPointer 均構(gòu)建于 XPath 表達(dá)式之上
XPath總體來(lái)說(shuō)功能很強(qiáng)大,而且查找效率別正則表達(dá)式要好很多,這里我們只用到一部分常用功能。
選取節(jié)點(diǎn)
XPath 使用路徑表達(dá)式在 XML 文檔中選取節(jié)點(diǎn)。節(jié)點(diǎn)是通過(guò)沿著路徑或者 step 來(lái)選取的。 下面列出了最有用的路徑表達(dá)式:

在下面的表格中,我們已列出了一些路徑表達(dá)式以及表達(dá)式的結(jié)果:

謂語(yǔ)(Predicates)
謂語(yǔ)用來(lái)查找某個(gè)特定的節(jié)點(diǎn)或者包含某個(gè)指定的值的節(jié)點(diǎn)。
謂語(yǔ)被嵌在方括號(hào)中。
在下面的表格中,我們列出了帶有謂語(yǔ)的一些路徑表達(dá)式,以及表達(dá)式的結(jié)果:

選取未知節(jié)點(diǎn)
XPath 通配符可用來(lái)選取未知的 XML 元素。

在下面的表格中,我們列出了一些路徑表達(dá)式,以及這些表達(dá)式的結(jié)果:

選取若干路徑
通過(guò)在路徑表達(dá)式中使用"|"運(yùn)算符,您可以選取若干個(gè)路徑。
在下面的表格中,我們列出了一些路徑表達(dá)式,以及這些表達(dá)式的結(jié)果:

模擬GitHub登錄獲取個(gè)人信息
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Created by master on 2018/7/2 16:21.
import requests
import re
from lxml import etree
user_headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36",
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh,en;q=0.9,zh-CN;q=0.8',
}
# html文檔下載模塊
def download_html(user, pwd):
login_url = 'https://github.com/login'
# 使用session自動(dòng)處理cookies
session = requests.Session()
# 獲取authenticity_token
response = session.get(login_url, headers=user_headers, verify=False)
pattern = re.compile(r'<input type="hidden" name="authenticity_token" value="(.*)" />')
authenticity_token = pattern.findall(response.text)[0]
# 登錄參數(shù)
login_data = {
'commit': 'Sign in',
'utf8': '?',
'authenticity_token': authenticity_token, 'login': user,
'password': pwd
}
# 登錄
session_url = 'https://github.com/session'
response = session.post(session_url, headers=user_headers, data=login_data, verify=False)
pattern = re.compile(r'Incorrect username or password')
if pattern.findall(response.text): # 密碼錯(cuò)誤
print("賬號(hào)或密碼錯(cuò)誤!")
return
else:
# 獲取user_id
pattern = re.compile(r'Signed in as <strong class="css-truncate-target">(.*)</strong>')
user_id = pattern.findall(response.text)[0]
# 請(qǐng)求個(gè)人信息頁(yè)面
response = session.get("https://github.com/" + user_id, headers=user_headers, verify=False)
return etree.HTML(response.text)
# 信息解析模塊
def parse_html(html):
if not html:
return
your_avatar = html.xpath("http://img[@class='avatar width-full rounded-2']/@src")[0]
your_nickname = html.xpath("http://span[@class='p-name vcard-fullname d-block overflow-hidden']/text()")[0]
your_user_id = html.xpath("http://span[@class='p-nickname vcard-username d-block']/text()")[0]
your_repositories = html.xpath("http://a[@class='UnderlineNav-item '][@title='Repositories']/span/text()")[0]
your_stars = html.xpath("http://a[@class='UnderlineNav-item '][@title='Stars']/span/text()")[0]
your_follows = html.xpath("http://a[@class='UnderlineNav-item '][@title='Followers']/span/text()")[0]
your_following = html.xpath("http://a[@class='UnderlineNav-item '][@title='Following']/span/text()")[0]
print("your avatar url: %s" % your_avatar)
print("your nick name: %s" % trim(your_nickname))
print("your user id: %s" % trim(your_user_id))
print("your repositories count: %s" % trim(your_repositories))
print("your starts count: %s" % trim(your_stars))
print("your follows count: %s" % trim(your_follows))
print("your fans count: %s" % trim(your_following))
# 去掉空格和換行
def trim(txt):
return txt.strip().replace("\n", "")
if __name__ == '__main__':
username = input("請(qǐng)輸入您的GitHub賬號(hào):")
password = input("請(qǐng)輸入您的GitHub密碼:")
html_doc = download_html(username, password)
parse_html(html_doc)
輸出
請(qǐng)輸入您的GitHub賬號(hào):**********
請(qǐng)輸入您的GitHub密碼:**********
your avatar url: https://avatars2.githubusercontent.com/u/11495586?s=460&v=4
your nick name: lxy
your user id: lxygithub
your repositories count: 29
your starts count: 60
your follows count: 2
your fans count: 6