博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
pyspider爬取TripAdvisor
阅读量:4699 次
发布时间:2019-06-09

本文共 1890 字,大约阅读时间需要 6 分钟。

1 #!/usr/bin/env python 2 # -*- encoding: utf-8 -*- 3 # Created on 2017-06-11 10:10:53 4 # Project: london 5  6 from pyspider.libs.base_handler import * 7 import pymongo 8  9 10 class Handler(BaseHandler):11     crawl_config = {12     }13     client = pymongo.MongoClient('localhost')14     db = client['trip']15 16     @every(minutes=24 * 60)17     def on_start(self):18         self.crawl('https://www.tripadvisor.cn/Attractions-g186338-Activities-c47-London_England.html', callback=self.index_page)19 20     @config(age=10 * 24 * 60 * 60)21     def index_page(self, response):22         for each in response.doc('.listing_title > a').items():23             self.crawl(each.attr.href, callback=self.detail_page)24         next_page = response.doc('.pagination .nav.next').attr.href25         self.crawl(next_page,callback = self.index_page)26 27     @config(priority=2)28     def detail_page(self, response):29         return {30             "name":response.doc('h1').text(),31             "url": response.url,32             'comment':response.doc('.heading_ratings .taLnk').text(),33             'address':response.doc('.addressReset > span.format_address').text(),34             'phone':response.doc('.phoneNumber').text(),35             'duration':response.doc('#MAP_AND_LISTING > div.main_section.listingbar > div > div.above_fold_listing_details > div > div:nth-child(5) > div > div:nth-child(1)').text(),36             'instruction':response.doc('#MAP_AND_LISTING > div.main_section.listingbar > div > div.above_fold_listing_details > div > div:nth-child(6) > div > b').text()37         }38     def on_result(self,result):39         if result:40             self.save_to_mongo(result)41             42     def save_to_mongo(self,result):43         if self.db['london'].insert(result):44             print('saved to mongo',result)45

 

转载于:https://www.cnblogs.com/themost/p/6985282.html

你可能感兴趣的文章
团队项目(第五周)
查看>>
SQL 优化经验总结34条
查看>>
开源 视频会议 收藏
查看>>
核心J2EE模式 - 截取过滤器
查看>>
.net开源CMS
查看>>
JdbcTemplate
查看>>
第一次使用maven记录
查看>>
SharePoint服务器端对象模型 之 使用CAML进展数据查询
查看>>
Building Tablet PC Applications ROB JARRETT
查看>>
Adobe® Reader®.插件开发
查看>>
【POJ 3461】Oulipo
查看>>
Alpha 冲刺 (5/10)
查看>>
使用Siege进行WEB压力测试
查看>>
斑马为什么有条纹?
查看>>
android多层树形结构列表学习笔记
查看>>
Android_去掉EditText控件周围橙色高亮区域
查看>>
《构建之法》第一、二、十六章阅读笔记
查看>>
arrow:让Python的日期与时间变的更好
查看>>
(转)Excel的 OleDb 连接串的格式(连接Excel 2003-2013)
查看>>
Java并发编程
查看>>