#得到指定一个URL的网页内容 def askURL(url): head = { #模拟浏览器头部信息,向服务器发消息 "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36" }
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 saveDate(datalist,savepath): print("save...") book = xlwt.Workbook(encoding="utf-8",style_compression=0) sheet = book.add_sheet('豆瓣电影Top250',cell_overwrite_ok=True) col = ("电影详情链接","图片链接","影片中文名","影片外国名","评分","评分人数","概括","相关信息") for i in range(0,8): sheet.write(0,i,col[i]) for i in range(0,250): print("第%d条" %(i+1)) data = datalist[i] for j in range(0,8): sheet.write(i+1,j,data[j])