爬虫练手项目:获取豆瓣评分最高的电影并下载
2020-01-15 11:05
246 查看
前期回顾
上篇博文我们学习了Python爬虫的四大库
urllib,
requests,
BeautifulSoup以及
selenium
爬虫常用库介绍
- 学习了
urllib
与request
的常见用法 - 学习了使用
BeautifulSoup
来解析网页以及使用selenium
来驱动浏览器
# 我们导入了 web 驱动模块 from selenium import webdriver # 接着我们创建了一个 Chrome 驱动 driver = webdriver.Chrome() # 接着使用 get 方法打开百度 driver.get("https://www.baidu.com") # 获取输入框并且往里面写入我们要搜索的内容 input = driver.find_element_by_css_selector('#kw') input.send_keys("波多野结衣照片") # 我们就获取到搜索这个按钮然后点击 button = driver.find_element_by_css_selector('#su') button.click()
则是上次查看波多老师图片的代码,效果如下
抓取豆瓣电影并保存本地
我们来抓取一下豆瓣上排名前250的电影
import requests from bs4 import BeautifulSoup import xlwt 加群:456926667,获取更多学习资料、练手项目,以及学习氛围 def request_douban(url): try: response = requests.get(url) if response.status_code == 200: return response.text except requests.RequestException: return None book = 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, '排名') sheet.write(0, 3, '评分') sheet.write(0, 4, '作者') sheet.write(0, 5, '简介') n = 1 def save_to_excel(soup): list = soup.find(class_='grid_view').find_all('li') for item in list: item_name = item.find(class_='title').string item_img = item.find('a').find('img').get('src') item_index = item.find(class_='').string item_score = item.find(class_='rating_num').string item_author = item.find('p').text if (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) global n sheet.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 def main(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__': for i in range(0, 10): main(i) book.save(u'豆瓣最受欢迎的250部电影.csv')
代码分析
首先导入相关库
import requests # 请求网页库 from bs4 import BeautifulSoup # 解析网页库 import xlwt # 与Excel文件交互
定义一个请求网页的函数
def request_douban(url): try: response = requests.get(url) if response.status_code == 200: return response.text except requests.RequestException: return None
创建一个存储数据的Excel
book = 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, '排名') sheet.write(0, 3, '评分') sheet.write(0, 4, '作者') sheet.write(0, 5, '简介') n = 1
定义一个将BeautifulSoup到的数据存入Excel的函数
def save_to_excel(soup): list = soup.find(class_='grid_view').find_all('li') for item in list: item_name = item.find(class_='title').string item_img = item.find('a').find('img').get('src') item_index = item.find(class_='').string item_score = item.find(class_='rating_num').string item_author = item.find('p').text if (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) global n sheet.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并且存储,调用主函数
def main(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__': for i in range(0, 10): main(i)
运行后发现文件夹中多了 “豆瓣最受欢迎的250部电影.csv”这个文件,打开看看
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- python爬虫之通过BeautifulSoup获取豆瓣最新上映电影的海报
- python爬虫之豆瓣电影评分
- 简单的PHP爬虫,获取豆瓣正在热映电影列表
- python BeautifulSoup 获取豆瓣当前热映电影以及评分
- python爬虫(实例1):requests+xpath 爬取并下载豆瓣电影的演员照片
- 一个简单的爬虫,爬取豆瓣上某部电影的所有评论及计算评分
- 爬虫之获取豆瓣电影排行榜(BeautifulSoup)
- Golang语言快速上手到综合实战(Go语言、Beego框架、高并发聊天室、豆瓣电影爬虫) 下载
- Python爬虫之多线程下载豆瓣Top250电影图片
- 用Scrapy爬虫下载图片(豆瓣电影图片)
- 爬虫实践——获取豆瓣TOP250电影
- python爬虫——获取豆瓣评分图书
- 爬虫实战【12】使用cookie登陆豆瓣电影以及获取单个电影的所有短评
- java爬虫获取豆瓣电影信息(解析网页部分)
- 获取豆瓣电影评分并保存到本地 遇到3个错误
- Python爬虫获取豆瓣电影TOP250
- 【Python成长之路】从零学爬虫--下载豆瓣8分以上的电影(附电影下载地址)
- 爬虫实战【11】Python获取豆瓣热门电影信息
- python爬虫之通过正则表达式获取豆瓣最新上映电影的海报
- Python爬虫之模拟登录豆瓣获取最近看过的电影