利用beautifulsoup爬取豆瓣电影top250,存储在mongodb

不多说了,上代码:

1 from requests import request 2 from bs4 import BeautifulSoup 3 import re 4 import pymongo 5 6 7 8 class SpiderDouBan: 9 10 11 12 def __init__(self): 13 client = pymongo.MongoClient(host='localhost', port=27017) 14 db = client['spider_db'] 15 self.collection = db['douban_movie_top250'] 16 17 18 19 def get_html(self, url): 20 ''' 21 获取一页的html文本 22 :param url: 地址 23 :return: 24 ''' 25 html = request('get', url).text 26 soup = BeautifulSoup(html, 'lxml') 27 return soup 28 29 30 def get_one_page(self, soup, order): 31 ''' 32 获取某一页的内容 33 :param soup: soup实例化对象 34 :return: 35 ''' 36 movie_names = [span.string for span in soup.find_all(name='span', attrs={'class': 'title'}) if not re.search('\/', span.string)] 37 movie_actors = [ re.sub('\n|\xa0', '', p.get_text().strip('" " |\n | \xa0')).split('/') for p in soup.find_all(name='p', attrs={'class': ''})] 38 movie_rates = [span.string for span in soup.find_all(name='span', attrs={'class': 'rating_num'})] 39 comment_num = [span_2.string for span in soup.find_all(attrs={'property': 'v:best'}) for span_2 in span.next_siblings if re.search('\w+', span_2.string)] 40 short_comments = [re.sub('', '', span.string) for span in soup.find_all(class_='inq')] 41 for index, name in enumerate(movie_names): 42 print(f'正在爬取第{order + index + 1}条数据...') 43 movie_info = { 44 'order': f'No.{order + index + 1}', 45 'movie_name': name, 46 'movie_type': f'{re.findall("[0-9]+", movie_actors[index][-3])[0]}年/{movie_actors[index][-2]}/{movie_actors[index][-1]}', 47 'movie_rate': f'{movie_rates[index][0]}分', 48 'short_comment': f'{short_comments[index]}' 49 } 50 self.collection.insert_one(movie_info) 51 52 53 54 def main(self, url, order): 55 ''' 56 主程序 57 :return: 58 ''' 59 soup = self.get_html(url) 60 self.get_one_page(soup, order) 61 62 63 64 65 if __name__ == '__main__': 66 for offset in range(0, 250, 25): 67 order = offset 68 url = f'https://movie.douban.com/top250?start={str(offset)}' 69 SpiderDouBan().main(url, order)

运行结果:

MongoDB存储效果:

 


__EOF__

本文作者cnhkzyy
本文链接https://www.cnblogs.com/my_captain/p/11072841.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   cnhkzyy  阅读(782)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2018-06-23 持续集成平台jenkins
点击右上角即可分享
微信分享提示