import?urllib.request
print("第一種抓取鏈接的內容的方法:")
url='http://www.baidu.com'
response1=urllib.request.urlopen(url)
html=response1.read()
print(response1.getcode())#獲取http狀態碼
print(len(html))#返回爬取內容的長度
print(html.decode('utf-8'))#輸入百度網頁對應的代碼
import?urllib.request
print("第二種抓取鏈接的內容的方法:")
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狀態碼
print(len(html))#返回爬取內容的長度
print(html.decode('utf-8'))#輸入百度網頁對應的代碼
import?urllib.request
from?http?import?cookiejar
print('第三種抓取鏈接內容的方法')
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狀態碼
print(len(html))#返回爬取內容的長度
print(html.decode('utf-8'))#輸入百度網頁對應的代碼
2019-07-31
2019-07-23
第三個代碼出錯提示,表示不解