02爬取豆瓣最受欢迎的250部电影
# 爬取豆瓣最受欢迎的250部电影,并写入Excel表格中 import requests,xlwt from bs4 import BeautifulSoup # 请求豆瓣网站,获取网页源码 def request_douban(url): try : # 请求url headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36"} response = requests.get(url,headers = headers) # 判断网页的返回码是不是200 print(response.status_code) if response.status_code == 200: return response.text except requests.RequestException: return None book = xlwt.Workbook(encoding = "utf-8",style_compression = 0 ) # 先定义一个Excel表格,写好名称,图片等信息 sheet = book.add_sheet("豆瓣电影Top250",cell