python獲取網頁信息的三種方法


import urllib.request
import http.cookiejar

url = 'http://www.baidu.com/'

# 方法一
print('方法一')
req_one = urllib.request.Request(url)
req_one.add_header('User-Agent', 'Mozilla/6.0')
res_one = urllib.request.urlopen(req_one)
code_one = res_one.getcode()
html_one = res_one.read().decode('utf-8')
res_one.close()
print('方法一網頁狀態碼:%s' % (code_one))
print('方法一網頁內容:'+html_one)


# 方法二
print('方法二')
res_two = urllib.request.urlopen(url)
code_two = res_two.getcode()
html_two = res_two.read().decode('utf-8')
print('方法二網頁狀態碼:%s' % (code_two))
print('方法二網頁內容:'+html_two)


#方法三
print('方法三')
cj = http.cookiejar.LWPCookieJar()
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
urllib.request.install_opener(opener)
res_three = urllib.request.urlopen(url)
print(cj)
code_three = res_three.getcode()
html_three = res_three.read().decode('utf-8')
res_three.close()
print('方法三網頁狀態碼:%s' % (code_three))
print('方法三的網頁內容:'+html_three)

 


免責聲明!

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



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