爬蟲技術(shù)-使用Requests抓取網(wǎng)頁(yè)內(nèi)容

requests

實(shí)現(xiàn)爬蟲第一步:數(shù)據(jù)抓取。

不知道從什么時(shí)候,貌似談到Python技術(shù), 必談爬蟲。

講到爬蟲也不得不說(shuō)到Python

Python這門語(yǔ)言對(duì)抓取網(wǎng)頁(yè)有什么相關(guān)的技術(shù)方案呢。

那本文就介紹如何實(shí)現(xiàn)抓取網(wǎng)頁(yè)內(nèi)容:RequestsHTTP庫(kù)的使用。

有了好用的工具,就可以愉快的搞事情啦。

安裝Requests

pip方式安裝

pip install requests

源碼安裝

git clone git://github.com/kennethreitz/requests.git

Requests使用

來(lái)感受下Requests如何使用

>>> r = requests.get('https://api.github.com/user', auth=('user', 'pass'))
>>> r.status_code
200
>>> r.headers['content-type']
'application/json; charset=utf8'
>>> r.encoding
'utf-8'
>>> r.text
u'{"type":"User"...'
>>> r.json()
{u'private_gists': 419, u'total_private_repos': 77, ...}

上面是官方文檔給出的示例。代碼很直觀,相信大家能看明白。

  • response.get方法: HTTP GET方式請(qǐng)求URL

  • r.status_code 響應(yīng)的HTTP狀態(tài)碼

  • r.headers :HTTP頭信息

  • r.encoding :編碼格式

  • r.text 網(wǎng)頁(yè)內(nèi)容

下面將通過(guò)請(qǐng)求、響應(yīng)的過(guò)程分別介紹Requests相應(yīng)的方法。

請(qǐng)求

導(dǎo)入模塊

import requests 

GET 請(qǐng)求:

r = requests.get('https://github.com/timeline.json')

POST 請(qǐng)求

r = requests.post("http://httpbin.org/post")

POST 上傳

url = 'http://httpbin.org/post'
files = {'file': open('report.xls', 'rb')}
r = requests.post(url, files=files)

其它HTTP方法

r = requests.put("http://httpbin.org/put")
r = requests.delete("http://httpbin.org/delete")
r = requests.head("http://httpbin.org/get")
r = requests.options("http://httpbin.org/get")

攜帶參數(shù)

payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.get("http://httpbin.org/get", params=payload)

payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.post("http://httpbin.org/post", data=payload)

import json
url = 'https://api.github.com/some/endpoint'
payload = {'some': 'data'}
r = requests.post(url, data=json.dumps(payload))

設(shè)置超時(shí)

r = requests.get('https://github.com', timeout=5)

禁止重定向

r = requests.get('http://github.com', allow_redirects=False)

設(shè)置請(qǐng)求頭

url = 'https://api.github.com/some/endpoint'
headers = {'user-agent': 'my-app/0.0.1'}
r = requests.get(url, headers=headers)

設(shè)置代理

import requests

proxies = {
  "http": "http://10.10.1.10:3128",
  "https": "http://10.10.1.10:1080",
}

requests.get("http://example.org", proxies=proxies)

發(fā)送Cookie

url = 'http://httpbin.org/cookies'
cookies = dict(cookies_are='working')

r = requests.get(url, cookies=cookies)
r.text
'{"cookies": {"cookies_are": "working"}}'
import requests
params = {'username': 'Ryan', 'password': 'password'}
r = requests.post("http://pythonscraping.com/pages/cookies/welcome.php", params) print("Cookie is set to:")
print(r.cookies.get_dict())
print("-----------")
print("Going to profile page...")
r = requests.get("http://pythonscraping.com/pages/cookies/profile.php",
                      cookies=r.cookies)
print(r.text)

保持回話

import requests
s = requests.Session()

s.get('http://httpbin.org/cookies/set/sessioncookie/123456789')
r = s.get("http://httpbin.org/cookies")

print(r.text)
# '{"cookies": {"sessioncookie": "123456789"}}'

import requests
session = requests.Session()
params = {'username': 'username', 'password': 'password'}
s = session.post("http://pythonscraping.com/pages/cookies/welcome.php", params) print("Cookie is set to:")
print(s.cookies.get_dict())
print("-----------")
print("Going to profile page...")
s = session.get("http://pythonscraping.com/pages/cookies/profile.php") print(s.text)

忽略SSL證書驗(yàn)證

requests.get('https://kennethreitz.com', verify=False)

響應(yīng)內(nèi)容

狀態(tài)碼

r = requests.get('http://httpbin.org/get')
>>> r.status_code

URL

r.url

響應(yīng)頭

r.headers
r.headers['Content-Type']
r.headers.get('content-type')

Cookie

r.cookies
r.cookies['example_cookie_name']

文本編碼

r.encoding

響應(yīng)內(nèi)容

r.text

二進(jìn)制內(nèi)容

r.content

JSON 響應(yīng)內(nèi)容

r.json()

原始響應(yīng)內(nèi)容

r.raw

以上內(nèi)容就是Requests的常用方法。 還有一些高級(jí)用法沒(méi)列舉,請(qǐng)查閱官方文檔。

官方文檔


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 目錄: Python網(wǎng)絡(luò)爬蟲(一)- 入門基礎(chǔ)Python網(wǎng)絡(luò)爬蟲(二)- urllib爬蟲案例Python網(wǎng)絡(luò)爬...
    一只寫程序的猿閱讀 13,141評(píng)論 17 68
  • 聲明:本文講解的實(shí)戰(zhàn)內(nèi)容,均僅用于學(xué)習(xí)交流,請(qǐng)勿用于任何商業(yè)用途! 一、前言 強(qiáng)烈建議:請(qǐng)?jiān)陔娔X的陪同下,閱讀本文...
    Bruce_Szh閱讀 13,005評(píng)論 6 28
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,562評(píng)論 19 139
  • 假設(shè)我們需要建造一棟建筑,那么第一步需要做什么工作?——沒(méi)錯(cuò),設(shè)計(jì)和規(guī)劃好地基,萬(wàn)丈高樓平地起依靠的是穩(wěn)健合理的地...
    汪繼志閱讀 913評(píng)論 0 10
  • 【佛珠的含義~第24顆】 太多時(shí)候,想象一種生活跟真的去過(guò)這種生活是兩回事。如果一樣?xùn)|西你得到了,卻覺得不過(guò)如此,...
    甘朝武閱讀 3,115評(píng)論 0 0

友情鏈接更多精彩內(nèi)容