python如何獲取網(wǎng)絡(luò)數(shù)據(jù)
Retrieving Data over HTTP
Python 內(nèi)置了 sockets 可以實(shí)現(xiàn)與網(wǎng)絡(luò)連接并通過(guò) Python 提取數(shù)據(jù)的功能。
socket 是可以提供雙向連接的,我們可以對(duì)同一個(gè) socket 進(jìn)行讀寫操作。比方說(shuō),A 對(duì) socket 寫入信息,并且將其發(fā)送給 socket 連接另一端 B;那么 B 讀取 socket 的內(nèi)容就可以得到 A 的信息。但是這樣會(huì)有一個(gè)問(wèn)題,比如說(shuō), A端并沒有發(fā)送任何信息,而 B 端一直在嘗試讀取 socket 的內(nèi)容,那么 A 端和 B 端只能陷入漫長(zhǎng)的等待。所以就引入了通信協(xié)議。協(xié)議通過(guò)規(guī)定誰(shuí)先發(fā)送,誰(shuí)后響應(yīng)等來(lái)規(guī)避上述的問(wèn)題。
import socket mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) mysock.connect(('fakeserver.com', 80)) # connect to server cmd = 'GET http://fakeserver.com/fake.txt HTTP/1.0\r\n\r\n'.encode() # send GET command followed by a blank line mysock.send(cmd) while True: # receive data and print out data = mysock.recv(512) if (len(data) < 1): break print(data.decode()) mysock.close()
Retrieving Data with urllib
利用 socket 我們可以與網(wǎng)站服務(wù)器,郵件服務(wù)器等建立連接。但是在建立連接之前,我們需要查詢文檔了解通信協(xié)議,然后根據(jù)協(xié)議編寫程序。所以相較于 socket 這種黑魔法,我們可以利用更為簡(jiǎn)單的 Python Package。
利用 urllib.urlopen()
打開網(wǎng)頁(yè)后,我們就可以讀取數(shù)據(jù),像讀取本地文件一樣。
import urllib.request fhand = urllib.request.urlopen('http://fakeserver.com/fake.txt') for line in fhand: #convert UTF-8 to unicode string and print out print(line.decode().strip())
因?yàn)?urllib 使用簡(jiǎn)潔方便,所以也常用與網(wǎng)絡(luò)爬蟲。網(wǎng)絡(luò)爬蟲除了要網(wǎng)頁(yè)讀取數(shù)據(jù)以外還需要在 HTML 格式中解釋出可用數(shù)據(jù),所以除了 urllib 還有另一常用利器就是 BeautifulSoup。
import urllib.request, urllib.parse, urllib.error from bs4 import BeautifulSoup import ssl # Ignore SSL certificate errors ctx = ssl.create_default_context() ctx.check_hostname = False ctx.verify_mode = ssl.CERT_NONE html = urllib.request.urlopen('http://fakeserver.com/fake.html', context=ctx).read() soup = BeautifulSoup(html, 'html.parser') tags = soup('a') # Retrieve all of the anchor tags for tag in tags: print(tag.get('href', None))
Retrieving Data from XML
在網(wǎng)絡(luò)交換數(shù)據(jù),我們常用的格式有兩種,一是 XML; 二是 JSON。
XML 長(zhǎng)得就像是 HTML 的近親,可以看做是樹的一種。利用 Python Package ElementTree 我們可以將 XML 文件轉(zhuǎn)換為樹,這樣可以方便我們后續(xù)提取有效的數(shù)據(jù)。
import xml.etree.ElementTree as ET data = ''' <person> <name>Jack</name> <phone>+123456789</phone> <email office="yes"/> </person> ''' tree = ET.fromstring(data) # convert xml into a tree print('Name:', tree.find('name').text) print('Attr:', tree.find('email').get('office'))
Retrieving Data from JSON
JSON 結(jié)構(gòu)相較于 XML 來(lái)說(shuō)更為簡(jiǎn)單,所以他的功能就沒有那么強(qiáng)大。但是 JSON 有一個(gè)優(yōu)勢(shì)就是可以直接映射到 Python 的 dictionaries 和 lists 中,非常實(shí)用。
我們可以直接利用 Python Package json 來(lái)解釋 JSON。
import json data = ''' { "name" : "Jack", "phone" : { "type" : "intl", "number" : "+123456789" }, "email" : { "office" : "yes" } } ''' info = json.loads(data) # convert json into a dictianary print('Name:', info['name']) print('Attr:', info['email']['office'])
作者:Yuki
出處:https://www.cnblogs.com/yukiwu/
以上就是python如何獲取網(wǎng)絡(luò)數(shù)據(jù)的詳細(xì)內(nèi)容,更多關(guān)于python獲取網(wǎng)絡(luò)數(shù)據(jù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- Python udp網(wǎng)絡(luò)程序?qū)崿F(xiàn)發(fā)送、接收數(shù)據(jù)功能示例
- Python大數(shù)據(jù)之網(wǎng)絡(luò)爬蟲的post請(qǐng)求、get請(qǐng)求區(qū)別實(shí)例分析
- Python 網(wǎng)絡(luò)編程之UDP發(fā)送接收數(shù)據(jù)功能示例【基于socket套接字】
- 詳解Python3網(wǎng)絡(luò)爬蟲(二):利用urllib.urlopen向有道翻譯發(fā)送數(shù)據(jù)獲得翻譯結(jié)果
- Python下載網(wǎng)絡(luò)文本數(shù)據(jù)到本地內(nèi)存的四種實(shí)現(xiàn)方法示例
- Python爬蟲實(shí)例_城市公交網(wǎng)絡(luò)站點(diǎn)數(shù)據(jù)的爬取方法
- python網(wǎng)絡(luò)編程調(diào)用recv函數(shù)完整接收數(shù)據(jù)的三種方法
- python網(wǎng)絡(luò)編程之?dāng)?shù)據(jù)傳輸U(kuò)DP實(shí)例分析
- python網(wǎng)絡(luò)編程學(xué)習(xí)筆記(九):數(shù)據(jù)庫(kù)客戶端 DB-API
相關(guān)文章
TensorFlow實(shí)現(xiàn)自定義Op方式
今天小編就為大家分享一篇TensorFlow實(shí)現(xiàn)自定義Op方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02matplotlib圖例、標(biāo)簽、坐標(biāo)軸刻度的字體設(shè)置方式
這篇文章主要介紹了matplotlib圖例、標(biāo)簽、坐標(biāo)軸刻度的字體設(shè)置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05Python辦公自動(dòng)化Word轉(zhuǎn)Excel文件批量處理
這篇文章主要為大家介紹了Python辦公自動(dòng)化Word轉(zhuǎn)Excel文件批量處理示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06