02爬取豆瓣最受欢迎的250部电影
2020-07-03 16:58
465 查看
# 爬取豆瓣最受欢迎的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_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
#将爬取下来的电影信息写入Excel表格中
def save_to_excel(soup):
# 将存放电影信息的li标签写入列表中
movie_lists = soup.find(class_ = "grid_view").find_all("li")
# 从列表中的源网页解析出电影的名称,作者等信息
for movie in movie_lists:
movie_name = movie.find(class_ = "title").string
movie_img = movie.find('a').find('img').get("src")
movie_index = movie.find(class_='').string
movie_score = movie.find(class_ = "rating_num").string
movie_author = movie.find('p').get_text()
movie_author = movie_author.replace(" ",'')
movie_author = movie_author.replace("\n",'')
if (movie.find(class_ = "inq") != None):
movie_intr = movie.find(class_ = "inq").string
print('爬取电影:' + movie_index + ' | ' + movie_name + ' | ' + movie_score + ' | '+movie_author + movie_intr)
# 将解析出的电影信息写入到Excel表格中
global n
sheet.write(n,0,movie_name)
sheet.write(n,1,movie_img)
sheet.write(n,2,movie_index)
sheet.write(n,3,movie_score)
sheet.write(n,4,movie_author)
sheet.write(n,5,movie_intr)
n = n + 1
# 定义主函数
def main(page):
# 定义请求网页的url链接
url = 'https://movie.douban.com/top250?start=' + str(page * 25) + '&filter='
# 请求网页
html = request_douban(url)
# print(html)
if html != None:
# 将收到的网页做一锅汤
soup = BeautifulSoup(html, "lxml")
save_to_excel(soup)
else:
print("请求网页失败")
if __name__ == "__main__":
for index in range(0,1):
main(index)
# 保存Excel表格
book.save(r'D:\python\豆瓣最受欢迎的250部电影.xls')
相关文章推荐
- 【Python】获取豆瓣Top250部电影信息
- requests爬取豆瓣前250部高分电影
- 爬取豆瓣电影排名前250部电影并且存入Mongo数据库
- python 利用scrapy爬取豆瓣TOP250部电影信息分别保存为csv、json、存入mysql、下载海报图片
- 微信小程序豆瓣电影项目的改造过程经验分享
- Java豆瓣电影爬虫——抓取电影详情和电影短评数据
- Python爬虫小案例:豆瓣电影TOP250
- Python使用mongodb保存爬取豆瓣电影的数据过程解析
- 利用AJAX JAVA 通过Echarts实现豆瓣电影TOP250的数据可视化
- Java豆瓣电影爬虫——小爬虫成长记(附源码)
- python爬取豆瓣电影信息
- [Python]抓取豆瓣电影列表的标题
- BeautifulSoup解析豆瓣即将上映的电影信息
- [python爬虫] BeautifulSoup和Selenium对比爬取豆瓣Top250电影信息
- python爬虫 Scrapy2-- 爬取豆瓣电影TOP250
- python爬取(自动化)豆瓣电影影评,并存储。
- 豆瓣电影数据获取APP(已完成)
- 小程序之豆瓣电影源码解读
- 【scrapy】scrapy按分类爬取豆瓣电影基础信息
- 豆瓣电影改版,翻页方式从页码变成加载更多,简易爬虫方式不灵了。求大神指教!