欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python爬蟲實(shí)現(xiàn)HTTP網(wǎng)絡(luò)請求多種實(shí)現(xiàn)方式

 更新時(shí)間:2020年06月19日 09:58:39   作者:夏日的向日葵  
這篇文章主要介紹了Python爬蟲實(shí)現(xiàn)HTTP網(wǎng)絡(luò)請求多種實(shí)現(xiàn)方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

1、通過urllib.requests模塊實(shí)現(xiàn)發(fā)送請求并讀取網(wǎng)頁內(nèi)容的簡單示例如下:

#導(dǎo)入模塊
import urllib.request
#打開需要爬取的網(wǎng)頁
response = urllib.request.urlopen('http://www.baidu.com')
#讀取網(wǎng)頁代碼
html = response.read()
#打印讀取的內(nèi)容
print(html)

結(jié)果:

b'<!DOCTYPE html><!--STATUS OK-->\n\n\n \n \n       <html><head><meta http-equiv="Content-Type" content="text/html;charset=utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"><meta content="always" name="referrer"><meta name="theme-color" content="#2932e1"><meta name="description" content="\xe5\x85\xa8\xe7\x90\x83\xe6\x9c\x80\xe5\xa4\xa7\xe7\x9a\x84\xe4\xb8\xad\xe6\x96\x87\xe6\x90\x9c\xe7\xb4\xa2\xe5\xbc\x95\xe6\x93\x8e\xe3\x80\x81\xe8\x87\xb4\xe5\x8a\x9b\xe4\xba\x8e\xe8\xae\xa9\xe7\xbd\x91\xe6\xb0\x91\xe6\x9b\xb4\xe4\xbe\xbf\xe6\x8d\xb7\xe5\x9c\xb0\xe8\x8e\xb7\xe5\x8f\x96\xe4\xbf\xa1\xe6\x81\xaf\xef\xbc\x8c\xe6\x89\xbe\xe5\x88\xb0\xe6\x89\x80\xe6\xb1\x82\xe3\x80\x82\xe7\x99\xbe\xe5\xba\xa6\xe8\xb6\x85\xe8\xbf\x87\xe5\x8d\x83\xe4\xba\xbf\xe7\x9a\x84\xe4\xb8\xad\xe6\x96\x87\xe7\xbd\x91\xe9\xa1\xb5\xe6\x95\xb0\xe6\x8d\xae\xe5\xba\x93\xef\xbc\x8c\xe5\x8f\xaf\xe4\xbb\xa5\xe7\x9e\xac\xe9\x97\xb4\xe6\x89\xbe\xe5\x88\xb0\xe7\x9b\xb8\xe5\x85\xb3\xe7\x9a\x84\xe6\x90\x9c\xe7\xb4\xa2\xe7\xbb\x93\xe6\x9e\x9c\xe3\x80\x82"><link rel="shortcut icon" href="/favicon.ico" rel="external nofollow" type="image/x-icon" /><link rel="search" type="application/opensearchdescription+xml" href="/content-search.xml" rel="external nofollow" title="\xe7\x99\xbe\xe5\xba\xa6\xe6\x90\x9c\xe7\xb4\xa2" /><link rel="icon" sizes="any" mask  rel="external nofollow" ><link rel="dns-prefetch"  rel="external nofollow" /><link rel="dns-prefetch"  rel="external nofollow" /><link rel="dns-prefetch"  rel="external nofollow" /><link rel="dns-prefetch"  rel="external nofollow" /><link rel="dns-prefetch"  rel="external nofollow" /><link rel="dns-prefetch"  rel="external nofollow" /><title>\xe7\x99\xbe\xe5\xba\xa6\xe4\xb8\x80\xe4\xb8\x8b\xef\xbc\x8c\xe4\xbd\xa0\xe5\xb0\xb1\xe7\x9f\xa5\xe9\x81\x93</title><style index="newi" type="text/css">#form .bdsug{top:39px}.bdsug{display:none;position:absolute;width:535px;background:#fff;border:1px solid 
………………(太多省略)

以上示例中是通過get請求方式獲取百度的網(wǎng)頁內(nèi)容。

下面是通過urllib.request模塊的post請求實(shí)現(xiàn)獲取網(wǎng)頁信息的內(nèi)容:

#導(dǎo)入模塊
import urllib.parse
import urllib.request
#將數(shù)據(jù)使用urlencode編碼處理后,再使用encoding設(shè)置為utf-8編碼
data = bytes(urllib.parse.urlencode({'word':'hello'}),encoding='utf-8')
#打開指定需要爬取的網(wǎng)頁
response = urllib.request.urlopen('http://httpbin.org/post',data=data)
html = response.read()
#打印讀取的內(nèi)容
print(html)

結(jié)果:

b'{\n "args": {}, \n "data": "", \n "files": {}, \n "form": {\n "word": "hello"\n }, \n "headers": {\n "Accept-Encoding": "identity", \n "Content-Length": "10", \n "Content-Type": "application/x-www-form-urlencoded", \n "Host": "httpbin.org", \n "User-Agent": "Python-urllib/3.7", \n "X-Amzn-Trace-Id": "Root=1-5ec3f607-00f717e823a5c268fe0e0be8"\n }, \n "json": null, \n "origin": "123.139.39.71", \n "url": "http://httpbin.org/post"\n}\n'

2、urllib3模塊

通過urllib3模塊實(shí)現(xiàn)發(fā)送網(wǎng)絡(luò)請求的示例代碼:

#導(dǎo)入模塊
import urllib3
#創(chuàng)建PoolManager對象,用于處理與線程池的連接以及線程安全的所有細(xì)節(jié)
http = urllib3.PoolManager()
#對需要爬取的網(wǎng)頁發(fā)送請求
response = http.request('GET','https://www.baidu.com/')
#打印讀取的內(nèi)容
print(response.data)

結(jié)果:

b'<!DOCTYPE html><!--STATUS OK-->\r\n<html>\r\n<head>\r\n\t<meta http-equiv="content-type" content="text/html;charset=utf-8">\r\n\t<meta http-equiv="X-UA-Compatible" content="IE=Edge">\r\n\t<link rel="dns-prefetch"  rel="external nofollow" />\r\n\t<link rel="dns-prefetch"  rel="external nofollow" />\r\n\t<link rel="dns-prefetch"  rel="external nofollow" />\r\n\t<link rel="dns-prefetch"  rel="external nofollow" />\r\n\t<link rel="dns-prefetch"  rel="external nofollow" />\r\n\t<link rel="dns-prefetch"  rel="external nofollow" />\r\n\t<link rel="dns-prefetch"  rel="external nofollow" />\r\n\t<link rel="dns-prefetch"  rel="external nofollow" />\r\n\t<title>\xe7\x99\xbe\xe5\xba\xa6\xe4\xb8\x80\xe4\xb8\x8b\xef\xbc\x8c\xe4\xbd\xa0\xe5\xb0\xb1\xe7\x9f\xa5\xe9\x81\x93</title>\r\n\t<link  rel="external nofollow" rel="stylesheet" type="text/css" />\r\n\t<!--[if lte IE 8]><style index="index" >#content{height:480px\\9}#m{top:260px\\9}</style><![endif]-->\r\n\t<!--[if IE 8]><style index="index" >#u1 a.mnav,#u1 a.mnav:visited{font-family:simsun}</style><![endif]-->\r\n\t<script>var hashMatch = document.location.href.match(/#+(.*wd=[^&].+)/);if (hashMatch && hashMatch[0] && hashMatch[1]) {document.location.replace("http://"+location.host+"/s?"+hashMatch[1]);}
…………………………(太多省略)

post請求實(shí)現(xiàn)獲取網(wǎng)頁信息的內(nèi)容:

#導(dǎo)入模塊
import urllib3
#創(chuàng)建PoolManager對象,用于處理與線程池的連接以及線程安全的所有細(xì)節(jié)
http = urllib3.PoolManager()
#對需要爬取的網(wǎng)頁發(fā)送請求
response = http.request('POST','http://httpbin.org/post',fields={'word':'hello'})
#打印讀取的內(nèi)容
print(response.data)

結(jié)果:

b'{\n "args": {}, \n "data": "", \n "files": {}, \n "form": {\n "word": "hello"\n }, \n "headers": {\n "Accept-Encoding": "identity", \n "Content-Length": "128", \n "Content-Type": "multipart/form-data; boundary=06ff68d7a4a22f600244a70bf9382ab2", \n "Host": "httpbin.org", \n "X-Amzn-Trace-Id": "Root=1-5ec3f8c3-9f33c46c1c1b37f6774b84f2"\n }, \n "json": null, \n "origin": "123.139.39.71", \n "url": "http://httpbin.org/post"\n}\n'

3、requests模塊

以GET請求方式為例,打印多種請求信息的代碼:

#導(dǎo)入模塊
import requests
#對需要爬取的網(wǎng)頁發(fā)送請求
response = requests.get('http://www.baidu.com')
#打印狀態(tài)碼
print('狀態(tài)碼:',response.status_code)
#打印請求url
print('url:',response.url)
#打印頭部信息
print('header:',response.headers)
#打印cookie信息
print('cookie:',response.cookies)
#以文本形式打印網(wǎng)頁源碼
print('text:',response.text)
#以字節(jié)流形式打印網(wǎng)頁源碼
print('content:',response.content)

結(jié)果:

狀態(tài)碼: 200
url: http://www.baidu.com/
header: {'Cache-Control': 'private, no-cache, no-store, proxy-revalidate, no-transform', 'Connection': 'keep-alive', 'Content-Encoding': 'gzip', 'Content-Type': 'text/html', 'Date': 'Tue, 19 May 2020 15:28:30 GMT', 'Last-Modified': 'Mon, 23 Jan 2017 13:27:32 GMT', 'Pragma': 'no-cache', 'Server': 'bfe/1.0.8.18', 'Set-Cookie': 'BDORZ=27315; max-age=86400; domain=.baidu.com; path=/', 'Transfer-Encoding': 'chunked'}
cookie: <RequestsCookieJar[<Cookie BDORZ=27315 for .baidu.com/>]>
text: <!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8>
………………(此處省略)
content: b'<!DOCTYPE html>\r\n<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8>
………………(此處省略)

以POST請求方式,發(fā)送HTTP網(wǎng)頁請求的示例:

#導(dǎo)入模塊
import requests
#表單參數(shù)
data = {'word':'hello'}
#對需要爬取的網(wǎng)頁發(fā)送請求
response = requests.post('http://httpbin.org/post',data=data)
#以字節(jié)流形式打印網(wǎng)頁源碼
print(response.content)

結(jié)果:

b'{\n "args": {}, \n "data": "", \n "files": {}, \n "form": {\n "word": "hello"\n }, \n "headers": {\n "Accept": "*/*", \n "Accept-Encoding": "gzip, deflate", \n "Content-Length": "10", \n "Content-Type": "application/x-www-form-urlencoded", \n "Host": "httpbin.org", \n "User-Agent": "python-requests/2.23.0", \n "X-Amzn-Trace-Id": "Root=1-5ec3fc97-965139d919e5a08e8135e731"\n }, \n "json": null, \n "origin": "123.139.39.71", \n "url": "http://httpbin.org/post"\n}\n'

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • python之while循環(huán)、無限循環(huán)用法及說明

    python之while循環(huán)、無限循環(huán)用法及說明

    這篇文章主要介紹了python之while循環(huán)、無限循環(huán)用法及說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • python 多線程與多進(jìn)程效率測試

    python 多線程與多進(jìn)程效率測試

    這篇文章主要介紹了python 多線程與多進(jìn)程效率測試,在Python中,計(jì)算密集型任務(wù)適用于多進(jìn)程,IO密集型任務(wù)適用于多線程、接下來看看文章得實(shí)例吧,需要的朋友可以參考一下喲
    2021-10-10
  • 對Python字符串中的換行符和制表符介紹

    對Python字符串中的換行符和制表符介紹

    下面小編就為大家分享一篇對Python字符串中的換行符和制表符介紹,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05
  • python的random模塊及加權(quán)隨機(jī)算法的python實(shí)現(xiàn)方法

    python的random模塊及加權(quán)隨機(jī)算法的python實(shí)現(xiàn)方法

    下面小編就為大家?guī)硪黄猵ython的random模塊及加權(quán)隨機(jī)算法的python實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-01-01
  • Python學(xué)習(xí)小技巧之列表項(xiàng)的排序

    Python學(xué)習(xí)小技巧之列表項(xiàng)的排序

    這篇文章主要給大家介紹了Python學(xué)習(xí)小技巧之列表項(xiàng)排序的相關(guān)資料,文中介紹的非常詳細(xì),需要的朋友們可以參借鑒,下面跟著小編一起來學(xué)習(xí)學(xué)習(xí)吧。
    2017-05-05
  • Numpy?數(shù)組索引的實(shí)現(xiàn)

    Numpy?數(shù)組索引的實(shí)現(xiàn)

    本文主要介紹了Numpy?數(shù)組索引的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • 新手學(xué)習(xí)Python2和Python3中print不同的用法

    新手學(xué)習(xí)Python2和Python3中print不同的用法

    在本篇文章里小編給大家分享的是關(guān)于Python2和Python3中print不同的用法,有興趣的朋友們可以學(xué)習(xí)下。
    2020-06-06
  • python程序輸出無內(nèi)容的解決方式

    python程序輸出無內(nèi)容的解決方式

    這篇文章主要介紹了python程序輸出無內(nèi)容的解決方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • python實(shí)現(xiàn)可下載音樂的音樂播放器

    python實(shí)現(xiàn)可下載音樂的音樂播放器

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)可下載音樂的音樂播放器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • Python的numpy選擇特定行列的方法

    Python的numpy選擇特定行列的方法

    這篇文章主要介紹了Python的numpy選擇特定行列的方法,有時(shí)需要抽取矩陣中特定行的特定列,比如,需要抽取矩陣x的0,1行的0,3列,結(jié)果為矩陣域,需要的朋友可以參考下
    2023-08-08

最新評論