一篇文章帶你搞定Python中urllib庫(操作URL)
Hey,大家好呀,我是Go進(jìn)階者。
一、操作URL
urllib提供了一系列用于操作URL的功能。分類講解相關(guān)內(nèi)容。
二、Get()
urllib的request模塊可以非常方便地抓取URL內(nèi)容,也就是發(fā)送一個(gè)GET請求到指定的頁面,然后返回HTTP的響應(yīng):
例如,對豆瓣的URLhttps://api.growingio.com/v2/22c937bbd8ebd703f2d8e9445f7dfd03/web/pv?stm=1593747087078進(jìn)行抓取,并返回響應(yīng):
- from urllib import request
- with request.urlopen('https://api.growingio.com/v2/22c937bbd8ebd703f2d8e9445f7dfd03/web/pv?stm=1593747087078') as f:
- data = f.read()
- print('Status:', f.status, f.reason)
- for k, v in f.getheaders():
- print('%s: %s' % (k, v))
- print('Data:', data.decode('utf-8'))
可以看到HTTP響應(yīng)的頭和JSON數(shù)據(jù):
如果要想模擬瀏覽器發(fā)送GET請求,就需要使用Request對象,通過往Request對象添加HTTP頭,就可以把請求偽裝成瀏覽器。例如,模擬iPhone 6去請求豆瓣首頁:
- from urllib import request
- req = request.Request('http://www.douban.com/')
- req.add_header('User-Agent', 'Mozilla/6.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/8.0 Mobile/10A5376e Safari/8536.25')
- with request.urlopen(req) as f:
- print('Status:', f.status, f.reason)
- for k, v in f.getheaders():
- print('%s: %s' % (k, v))
- print('Data:', f.read().decode('utf-8'))
這樣豆瓣會返回適合iPhone的移動版網(wǎng)頁:
三、Post()
如果要以POST發(fā)送一個(gè)請求,只需要把參數(shù)data以bytes形式傳入。
模擬一個(gè)微博登錄,先讀取登錄的郵箱和口令,然后按照weibo.cn的登錄頁的格式以username=xxx&password=xxx的編碼傳入:
- from urllib import request, parse
- print('Login to weibo.cn...')
- #電子郵件
- email = input('Email: ')
- #密碼
- passwd = input('Password: ')
- #相關(guān)的參數(shù)
- login_data = parse.urlencode([
- ('username', email),
- ('password', passwd),
- ('entry', 'mweibo'),
- ('client_id', ''),
- ('savestate', '1'),
- ('ec', ''),
- ('pagerefer', 'https://passport.weibo.cn/signin/welcome?entry=mweibo&r=http%3A%2F%2Fm.weibo.cn%2F')
- ])
- #網(wǎng)址請求
- req = request.Request('https://passport.weibo.cn/sso/login')
- req.add_header('Origin', 'https://passport.weibo.cn')
- #構(gòu)造User-Agent
- req.add_header('User-Agent', 'Mozilla/6.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/8.0 Mobile/10A5376e Safari/8536.25')
- req.add_header('Referer', 'https://passport.weibo.cn/signin/login?entry=mweibo&res=wel&wm=3349&r=http%3A%2F%2Fm.weibo.cn%2F')
- with request.urlopen(req, data=login_data.encode('utf-8')) as f:
- print('Status:', f.status, f.reason)
- for k, v in f.getheaders():
- print('%s: %s' % (k, v))
- print('Data:', f.read().decode('utf-8'))
如果登錄成功,獲得的響應(yīng)如下:
如果登錄失敗,獲得的響應(yīng)如下:
四、Handler
如果還需要更復(fù)雜的控制,比如通過一個(gè)Proxy去訪問網(wǎng)站,需要利用ProxyHandler來處理,示例代碼如下:
- import urllib.request
- # 構(gòu)建了兩個(gè)代理Handler,一個(gè)有代理IP,一個(gè)沒有代理IP
- httpproxy_handler = urllib.request.ProxyHandler({"https": "27.191.234.69:9999"})
- nullproxy_handler = urllib.request.ProxyHandler({})
- # 定義一個(gè)代理開關(guān)
- proxySwitch = True
- # 通過 urllib.request.build_opener()方法使用這些代理Handler對象,創(chuàng)建自定義opener對象
- # 根據(jù)代理開關(guān)是否打開,使用不同的代理模式
- if proxySwitch:
- opener = urllib.request.build_opener(httpproxy_handler)
- else:
- opener = urllib.request.build_opener(nullproxy_handler)
- request = urllib.request.Request("http://www.baidu.com/")
- # 1. 如果這么寫,只有使用opener.open()方法發(fā)送請求才使用自定義的代理,而urlopen()則不使用自定義代理。
- response = opener.open(request)
- # 2. 如果這么寫,就是將opener應(yīng)用到全局,之后所有的,不管是opener.open()還是urlopen() 發(fā)送請求,都將使用自定義代理。
- # urllib.request.install_opener(opener)
- # response = urllib.request.urlopen(request)
- # 獲取服務(wù)器響應(yīng)內(nèi)容
- html = response.read().decode("utf-8")
- # 打印結(jié)果
- print(html)
如果代理成功返回網(wǎng)址的信息。
如果網(wǎng)址出錯(cuò)或者代理地址有誤,返回下面界面。
五、總結(jié)
使用Python語言,能夠幫助大家更好的學(xué)習(xí)Python。urllib提供的功能就是利用程序去執(zhí)行各種HTTP請求。如果要模擬瀏覽器完成特定功能,需要把請求偽裝成瀏覽器。偽裝的方法是先監(jiān)控瀏j覽器發(fā)出的請求,再根據(jù)瀏覽器的請求頭來偽裝,User-Agent頭就是用來標(biāo)識瀏覽器的。