当前位置: 首页 > 后端技术 > Python

爬虫实践项目:获取豆瓣评分最高的电影并下载

时间:2023-03-25 23:36:54 Python

回顾上一篇博文,我们学习了Python爬虫的四大库,urllib、requests、BeautifulSoup、selenium爬虫常用库。我们学习了urllib和request的常用用法。我们学习了如何使用BeautifulSoup解析网页和使用selenium驱动浏览器#我们导入添加了web驱动模块fromseleniumimportwebdriver#然后我们创建了一个Chrome驱动driver=webdriver.Chrome()#然后使用get方法打开百度driver.get("https://www.baidu.com")#获取输入框并在input=driver.find_element_by_css_selector('#kw')input.send_keys("HatanoYuiphoto")#我们将获取搜索按钮并点击button=driver.find_element_by_css_selector('#su')button.click()是上次查看博多老师图片的代码,效果如下抓取豆瓣电影并保存他们在本地抓取豆瓣前250部电影importrequestsfrombs4importBeautifulSoupimportxlwt加群:456926667,获取更多学习资料、实践项目、学习氛围defrequest_douban(url):try:response=requests.get(url)ifresponse.status_code==200:returnresponse.text除了请求ts.RequestException:returnNonebook=xlwt.Workbook(encoding='utf-8',style_compression=0)sheet=book.add_sheet('豆瓣电影Top250',cell_overwrite_ok=True)sheet.write(0,0,'Name')sheet.write(0,1,'图片')sheet.write(0,2,'排名')sheet.write(0,3,'评分')sheet.write(0,4,'作者')sheet.write(0,5,'简介')n=1defsave_to_excel(soup):list=soup.find(class_='grid_view').find_all('li')列表中的项目:item_name=item.find(class_='title').stringitem_img=item.find('a').find('img').get('src')item_index=item.find(class_='').stringitem_score=item.find(class_='rating_num').stringitem_author=项目。find('p').textif(item.find(class_='inq')!=None):item_intr=item.find(class_='inq').string#print('刷取电影:'+item_index+'|'+item_name+'|'+item_img+'|'+item_score+'|'+item_author+'|'+item_intr)print('刷取电影:'+item_index+'|'+item_name+'|'+item_score+'|'+item_intr)globalnsheet.write(n,0,item_name)sheet.write(n,1,item_img)sheet.write(n,2,item_index)sheet.write(n,3,item_score)sheet.write(n,4,item_author)sheet.write(n,5,item_intr)n=n+1defmain(page):url='https://movie.douban.com/top250?start='+str(page*25)+'&filter='html=request_douban(url)soup=BeautifulSoup(html,'lxml')save_to_excel(soup)if__name__=='__main__':foriinrange(0,10):main(i)book.save(u'豆瓣最受欢迎的250部电影.csv')代码分析先导入相关库importrequests#requeststheweblibraryfrombs4importBeautifulSoup#解析weblibraryimportxlwt#与Excel文件交互定义一个request网页函数defrequest_douban(url):try:response=requests.get(url)ifresponse.status_code==200:returnresponse.textexceptrequests.RequestException:returnNone创建一个存储数据的Excelbook=xlwt.Workbook(encoding='utf-8',style_compression=0)sheet=book.add_sheet('豆瓣电影Top250',cell_overwrite_ok=True)sheet.write(0,0,'名字')sheet.write(0,1,'图片')sheet.write(0,2,'Rank')sheet.write(0,3,'Rating')sheet.write(0,4,'Author')sheet.write(0,5,'Introduction')n=1定义一个BeautifulSoup的数据存入Excel的函数defsave_to_excel(soup):list=soup.find(class_='grid_view').find_all('li')foriteminlist:item_name=item.find(class_='title').stringitem_img=item.find('a').find('img').get('src')item_index=item.find(class_='').stringitem_score=item.find(class_='rating_num').字符串item_author=item.find('p').textif(item.find(class_='inq')!=None):item_intr=item.find(class_='inq').string#print('刷取电影:'+item_index+'|'+item_name+'|'+item_img+'|'+item_score+'|'+item_author+'|'+item_intr)print('刷取电影:'+item_index+'|'+item_name+'|'+item_score+'|'+item_intr)globalnsheet.write(n,0,item_name)sheet.write(n,1,item_img)sheet.write(n,2,item_index)sheet.write(n,3,item_score)sheet.write(n,4,item_author)sheet.write(n,5,item_intr)n=n+1定义主函数输入URL并且存储调用主函数defmain(page):url='https://movie.douban.com/top250?start='+str(page*25)+'&filter='html=request_douban(url)soup=BeautifulSoup(html,'lxml')save_to_excel(soup)if__name__=='__main__':foriinrange(0,10):运行main(i)后发现多了“豆瓣最火的250部电影文件夹中的“.csv”文件,打开

猜你喜欢