1打开编辑器
2撸几行代码
1 import urllib.request 2 3 import urllib.error 4 5 def main(): 6 7 askURl("http://movie.douban.com/top250?start") 8 9 #URL的网页内容 10 def askURl(url): 11 12 head = { 13 "User-Agent": "Mozilla/5.0(Windows NT 10.0;WOW64) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 80.0.3987.122Safari / 537.36" 14 } 15 #head伪装一下 告诉机器 16 17 request = urllib.request.Request(url,headers=head) 18 html = "" 19 try: 20 response = urllib.request.urlopen(request) 21 html = response.read().decode("utf-8") 22 print(html) 23 except urllib.error.URLError as e: 24 if hasattr(e,"code"): 25 print(e.code) 26 if hasattr(e,"reason"): 27 print(e.reason) 28 29 if __name__ == '__main__': 30 #调用函数 31 main()
浙公网安备 33010602011771号