python urlopen


Python urllib 庫提供了一個從指定的 URL 地址獲取網頁數據,然后對其進行分析處理,獲取想要的數據。

urlopen返回 一個類文件對象(fd),它提供了如下方法:
read() , readline() , readlines() , fileno() , close() :這些方法的使用方式與文件對象完全一樣;

info():返回一個httplib.HTTPMessage 對象,表示遠程服務器返回的頭信息(header)

getcode():返回Http狀態碼。如果是http請求,200表示請求成功完成;404表示網址未找到;
geturl():返回請求的url;

 

from urllib.request import urlopen
import json
from pprint import pprint
u=urlopen('https://www.baidu.com/').read() #get all content on url page
u1=urlopen('https://www.baidu.com/')
print(u1.info()) #get header information from remote server
print(u1.getcode())#get status code
print(u1.geturl())#get request url

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM