Requests如何在Python爬蟲中實現get請求?
作為Requests也算比較常見的庫,之前也講了一些基礎的知識,所以接下來我們會更將一些例項以及進階的知識,幫助大家快速成長。上一篇文章已經講過了post請求的實現,本篇要帶來的是get請求。有興趣的小夥伴可以把兩篇拿出來對比下,看看有什麼不同的地方。這裡就不再過多敘述了。
最基本的GET 請求可以直接用get方法
r = requests.get("")
如果想要加引數,可以利用params引數
import requests payload = {'key1': 'value1', 'key2': 'value2'} r = requests.get("", params=payload) print r.url
執行結果
?key2=value2&key1=value1
如果想請求 JSON 檔案,可以利用 json () 方法解析 例如自己寫一個 JSON 檔案命名為 a.json,內容如下
["foo", "bar", { "foo": "bar" }]
利用如下程式請求並解析
import requests r = requests.get("a.json") print r.text print r.json()
執行結果如下,其中一個是直接輸出內容,另外一個方法是利用 json () 方法解析,感受下它們的不同
["foo", "bar", { "foo": "bar" }] [u'foo', u'bar', {u'foo': u'bar'}]
以上就是Python爬蟲中Requests實現get請求的方法。相比較於post,get的請求更加清晰明瞭,每一步的程式碼也不會很長。如果讓小編選擇的話,會更傾向於get。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/4328/viewspace-2832296/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 4.爬蟲 requests庫講解 GET請求 POST請求 響應爬蟲
- Python Beautiful Soup+requests實現爬蟲Python爬蟲
- 爬蟲快速入門——Get請求的使用爬蟲
- python爬蟲請求頭Python爬蟲
- Python爬蟲教程-06-爬蟲實現百度翻譯(requests)Python爬蟲
- python爬蟲requests模組Python爬蟲
- Python網路爬蟲第三彈《爬取get請求的頁面資料》Python爬蟲
- Python爬蟲(二)——傳送請求Python爬蟲
- Python3爬蟲實戰(requests模組)Python爬蟲
- Python requests爬蟲例項Python爬蟲
- python3 實現 get 和 post 請求Python
- Datawhale-爬蟲-Task1(學習get與post請求)爬蟲
- Python 爬蟲實戰(二):使用 requests-htmlPython爬蟲HTML
- Python 爬蟲實戰(一):使用 requests 和 BeautifulSoupPython爬蟲
- python爬蟲之一:requests庫Python爬蟲
- 爬蟲——Requests模組爬蟲
- 爬蟲-Requests模組爬蟲
- Python爬蟲基礎-01-帶有請求引數的爬蟲Python爬蟲
- python+pytest介面自動化(4)-requests傳送get請求Python
- 爬蟲之requests庫爬蟲
- Python中get、post請求詳解(HTTP請求頭、狀態碼)PythonHTTP
- python爬蟲常用庫之requests詳解Python爬蟲
- 爬蟲中的TCP請求自動切換ip爬蟲TCP
- 如何在 PyQt 中實現非同步資料庫請求QT非同步資料庫
- SpringMVC中如何傳送GET請求、POST請求、PUT請求、DELETE請求。SpringMVCdelete
- python爬蟲利用requests製作代理池sPython爬蟲
- JB的Python之旅-爬蟲篇--requests&ScrapyPython爬蟲
- Python 爬蟲入門 (二) 使用Requests來爬取圖片Python爬蟲
- cURL實現傳送Get和Post請求(PHP)PHP
- 原生js實現Ajax請求,包含get和postJS
- PHP中使用cURL實現Get和Post請求PHP
- python爬蟲:爬蟲的簡單介紹及requests模組的簡單使用Python爬蟲
- Python實現微博爬蟲,爬取新浪微博Python爬蟲
- 爬蟲——爬取貴陽房價(Python實現)爬蟲Python
- python多執行緒非同步爬蟲-Python非同步爬蟲試驗[Celery,gevent,requests]Python執行緒非同步爬蟲
- 爬蟲中網路請求的那些事之urllib庫爬蟲
- 【爬蟲】第二章-基本請求庫爬蟲
- Python2爬蟲利器:requests庫的基本用法Python爬蟲