日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

爬取豆瓣电影Top 250封面

發(fā)布時(shí)間:2024/8/1 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 爬取豆瓣电影Top 250封面 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

大家好,作為一名互聯(lián)網(wǎng)行業(yè)的小白,寫博客只是為了鞏固自己學(xué)習(xí)的知識(shí),但由于水平有限,博客中難免會(huì)有一些錯(cuò)誤出現(xiàn),有不妥之處懇請(qǐng)各位大佬指點(diǎn)一二!
博客主頁(yè):鏈接: https://blog.csdn.net/weixin_52720197?spm=1018.2118.3001.5343

import requests from lxml import etree import pandas as pd import osMOVIES = [] IMGURLS = []# 獲取源代碼 def get_html(url):headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'}try:html = requests.get(url,headers = headers)html.encoding = html.apparent_encodingif html.status_code == 200:print('成功獲取源代碼')# print(html.text)except Exception as e:print('獲取源代碼失敗:%s' % e)return html.text# 數(shù)據(jù)解析 def parse_html(html):movies = [] # 電影列表imgurls = [] # 圖片列表html = etree.HTML(html)# 獲取lis的信息lis = html.xpath("//ol[@class = 'grid_view']/li")# 每寫一個(gè)Xpath,要run一下,看看有沒有數(shù)據(jù),或者調(diào)整格式for li in lis:# 姓名在<li>標(biāo)簽下的<span>標(biāo)簽name = li.xpath(".//a/span[@class='title'][1]/text()")[0]# split()可以去掉空格director_actor = "".join(li.xpath(".//div[@class='bd']/p/text()[1]")[0].replace(' ','').replace('\n','').replace('/','').split())info = "".join(li.xpath(".//div[@class='bd']/p/text()[2]")[0].replace(' ','').replace('\n','').split())# 評(píng)分rating_score = li.xpath(".//span[@class='rating_num']/text()")[0]# 評(píng)分人數(shù)rating_num = li.xpath(".//div[@class='star']/span[4]/text()")[0]# 簡(jiǎn)介introduce = li.xpath(".//p[@class='quote']/span/text()")if introduce:movie = {'name': name, 'director_actor': director_actor, 'info': info, 'rating_score': rating_score,'rating_num': rating_num, 'introduce': introduce[0]}else:movie = {'name': name, 'director_actor': director_actor, 'info': info, 'rating_score': rating_score,'rating_num': rating_num, 'introduce': None}# 獲取圖片imgurl = li.xpath(".//img/@src")[0]movies.append(movie)imgurls.append(imgurl)return movies,imgurlsdef download_img(url,movie):# 如果在此路徑下if 'movieposter' in os.listdir(r'D:\Python\spader\爬蟲\豆瓣電影封面'): # 自己定義路徑pass # 跳過else: # 否則os.mkdir('movieposter') # 建立movieposter文件夾os.chdir(r'D:\Python\spader\爬蟲\豆瓣電影封面\movieposter')img = requests.get(url).content# 以電影名+jpg,以wb形式寫入with open(movie['name'] + '.jpg','wb') as f: # 以電影名字命名print('正在下載 : %s' % url) # 提示信息f.write(img)if __name__ == '__main__':for i in range(10):url = 'https://movie.douban.com/top250?start=' + str(i * 25) + '&filter='# 獲取源代碼html = get_html(url)movies = parse_html(html)[0]imgurls = parse_html(html)[1]MOVIES.extend(movies)IMGURLS.extend(imgurls)for i in range(250):download_img(IMGURLS[i],MOVIES[i])os.chdir(r'D:\Python\spader\爬蟲\豆瓣電影封面')# 保存到csv文件中moviedata = pd.DataFrame(MOVIES)moviedata.to_csv('movie.csv') # 自己起名字print('電影信息成功保存到本地')

結(jié)果


解決文字亂碼
用記事本打開CSV文件----->文件------>另存為------>點(diǎn)編碼------>選擇ANSI----->保存,然后用excel打開就不會(huì)是亂碼了。

總結(jié)

以上是生活随笔為你收集整理的爬取豆瓣电影Top 250封面的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。