# 爬取豆瓣最受歡迎的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')
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/102172.html
標籤:Python
上一篇:獲取標題和內容
