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