使用pyquery爬取豆瓣电影top250,存储在mongodb

直接上代码

1 from pyquery import PyQuery as pq 2 from requests import request 3 import re 4 import pymongo 5 6 7 class SpiderDouBan2: 8 9 10 def __init__(self): 11 client = pymongo.MongoClient(host='localhost', port=27017) 12 db = client['spider_db'] 13 self.collection = db['douban_movie_top250_2'] 14 15 16 17 def get_doc(self, url): 18 ''' 19 获取某一页的doc 20 :param url: 地址 21 :return: 22 ''' 23 doc = pq(url) 24 return doc 25 26 27 def get_one_page(self, doc, order): 28 ''' 29 获取某一页的内容 30 :return: 31 ''' 32 movie_names = [name.text() for name in doc('.title').items() if not re.search('\/', name.text())] 33 movie_actors = [re.sub('\xa0|" "', '', actor.text()).split('\n') for actor in doc('.info .bd')('p:first-child').items()] 34 movie_rates = [rate.text() for rate in doc('.rating_num').items()] 35 comment_nums = [comment_num.text() for comment_num in doc('.star')('span:last-child').items()] 36 short_comments = [short_comment.text()[:-1] for short_comment in doc('.inq').items()] 37 for index, name in enumerate(movie_names): 38 print(f'正在爬取第{order + index + 1}条数据...') 39 movie_info = { 40 'order': f'No.{order + index + 1}', 41 'movie_name': name, 42 'movie_actor': movie_actors[index][0].rstrip('\/'), 43 'movie_type': movie_actors[index][1], 44 'movie_rate': f'{movie_rates[index]}分', 45 'comment_num': comment_nums[index], 46 'short_comment': short_comments[index] 47 } 48 self.collection.insert_one(movie_info) 49 50 51 52 def main(self, url, order): 53 ''' 54 主程序 55 :return: 56 ''' 57 doc = self.get_doc(url) 58 self.get_one_page(doc, order) 59 60 61 if __name__ == '__main__': 62 for offset in range(0, 250, 25): 63 order = offset 64 url = f'https://movie.douban.com/top250?start={str(offset)}' 65 SpiderDouBan2().main(url, order)

运行结果

 

 


__EOF__

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