Python用Pyspider爬取TripAdvisor的景点信息

江一帆 2020-03-10 PM 2569℃ 0条 2469字 Site load time is:234 ms 百度:已收录

先上效果图
Snipaste_2020-03-10_13-14-26.png

Snipaste_2020-03-10_13-14-49.png

Snipaste_2020-03-10_13-15-23.png

Snipaste_2020-03-10_13-21-54.png

上代码:

from pyspider.libs.base_handler import *
import pymongo

class Handler(BaseHandler):
    crawl_config = {
    }

    client = pymongo.MongoClient('localhost')
    db = client['TripAdvise']

    @every(minutes=24 * 60)
    def on_start(self):
        self.crawl('https://www.tripadvisor.cn/Attractions-g186338-Activities-c47-London_England.html',callback=self.index_page, validate_cert=False)

    @config(age=10 * 24 * 60 * 60)
    def index_page(self, response):
        for each in response.doc('div.listing_title > a').items():
            self.crawl(each.attr.href, callback=self.detail_page, validate_cert=False)
        # 翻页
        next = response.doc('.pagination .nav.next').attr.href
        self.crawl(next, callback=self.index_page, validate_cert=False)

    @config(priority=2)
    def detail_page(self, response):
        url = response.url
        name = response.doc('.h1').text()
        rating = response.doc(' div.ratingContainer > a > span').text()
        garde = response.doc('div.section.rating > span').text()
        address = response.doc('.contactInfo > .address').text()
        phone = response.doc('div.contact > div.contactType.phone.is-hidden-mobile > div').text()
        opening = response.doc('div.prw_rup.prw_common_atf_header_bl_responsive.headerBL > div > span').text()
        introduction = response.doc('.centerWell > div > div > div > div > div > span').text()
        return {
            "url": url,
            "name": name,
            "rating": rating,
            'garde': garde,
            "address": address,
            "phone": phone,
            "opening": opening,
            "introduction": introduction
        }

    def on_result(self, result):
        if result:
            self.save_to_mongo(result)

    def save_to_mongo(self, result):
        if self.db['london'].insert(result):
            print('保存到MongoDB成功', result)

参考:https://cuiqingcai.com/8317.html

标签: 爬虫

本文最后更新于:2020-03-10 13:22,可能因经年累月而与现状有所差异。

非特殊说明,本博所有文章均为博主原创。

评论啦~


召唤看板娘