Urllib簡介
發送請求
請求頭
請求頭參考我的另一篇博客:https://blog.csdn.net/LOG_IN_ME/article/details/81940205
IP代理
import urllib.request
url='http://movie.douban.com'
#設置代理ip
proxy_handler=urllib.request.ProxyHandler({
'http':'218.56.132.157:8080',
'https':'183.30.197.29:9797'
})
#必須使用build_opener()函數來創建帶有代理ip功能的opener對象
opener=urllib.request.build_opener(proxy_handler)
response=opener.open(url)
#讀取返回的內容
html=response.read().decode('utf-8')
#寫入txt
f = open('html.txt', 'w', encoding='utf-8')
f.write(html)
f.close()