# response = urllib.request.urlopen("http://www.baidu.com") # # print(response.status) # print(response.getheader("Server")) # url = "https://www.douban.com" # url = "http://httpbin.org/pos" # data = bytes(urllib.parse.urlencode({'name':'eric'}),encoding="utf-") # headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:103.0) Gecko/20100101 Firefox/103.0"}#伪装 # req = urllib.request.Request(url=url,data=data,headers=headers,method ="POST") # response = urllib.request.urlopen(req) # print(req) # url = "https://www.douban.com" # headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:103.0) Gecko/20100101 Firefox/103.0"}#伪装 # req = urllib.request.Request(url=url,headers=headers) # response = urllib.request.urlopen(req) # print(response.read().decode("utf-8"))
#得到指定URL的网页内容 def askURL(url): #用户代理 head = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:103.0) Gecko/20100101 Firefox/103.0"} request = urllib.request.Request(url,headers = head) html = "" try: response = urllib.request.urlopen(request) html = response.read().decode("utf-8") print(html) except urllib.error.URLError as e: if hasattr(e,"code"): print(e.code) if hasattr(e,"reason"): print(e.reason) #return html def main(): baseurl = "https://movie.douban.com/top250" askURL(baseurl)