import?urllib.request
print("第一種抓取鏈接的內(nèi)容的方法:")
url='http://www.baidu.com'
response1=urllib.request.urlopen(url)
html=response1.read()
print(response1.getcode())#獲取http狀態(tài)碼
print(len(html))#返回爬取內(nèi)容的長(zhǎng)度
print(html.decode('utf-8'))#輸入百度網(wǎng)頁(yè)對(duì)應(yīng)的代碼
import?urllib.request
print("第二種抓取鏈接的內(nèi)容的方法:")
url='http://www.baidu.com'
request=urllib.request.Request(url)
request.add_header("User_Agent","Mozilla/5.0")
response2=urllib.request.urlopen(url)
html=response2.read()
print(response2.getcode())#獲取http狀態(tài)碼
print(len(html))#返回爬取內(nèi)容的長(zhǎng)度
print(html.decode('utf-8'))#輸入百度網(wǎng)頁(yè)對(duì)應(yīng)的代碼
import?urllib.request
from?http?import?cookiejar
print('第三種抓取鏈接內(nèi)容的方法')
url='http://www.baidu.com'
cj=cookiejar.CookieJar
opener=urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
urllib.request.install_opener(opener)
response3=urllib.request.urlopen(url)
html=response3.read()
print(response3.getcode())#獲取http狀態(tài)碼
print(len(html))#返回爬取內(nèi)容的長(zhǎng)度
print(html.decode('utf-8'))#輸入百度網(wǎng)頁(yè)對(duì)應(yīng)的代碼
2019-07-31
2019-07-23
第三個(gè)代碼出錯(cuò)提示,表示不解