zoukankan      html  css  js  c++  java
  • pyspider爬取tripadvisor

    首先装pymongo,pyspider,具体安装方法不讲解,然后

    命令行下执行

    这句命令的意思是,运行 pyspider 并 启动它的所有组件。

    可以发现程序已经正常启动,并在 5000 这个端口运行。

    下来在浏览器中输入 http://localhost:5000,可以看到 PySpider 的主界面,点击右下角的 Create,命名为 pyshiderlianxi,当然名称你可以随意取,继续点击 Create。

    整个页面分为两栏,左边是爬取页面预览区域,右边是代码编写区域。下面对区块进行说明:

    左侧绿色区域:这个请求对应的 JSON 变量,在 PySpider 中,其实每个请求都有与之对应的 JSON 变量,包括回调函数,方法名,请求链接,请求数据等等。

    绿色区域右上角Run:点击右上角的 run 按钮,就会执行这个请求,可以在左边的白色区域出现请求的结果。

    左侧 enable css selector helper: 抓取页面之后,点击此按钮,可以方便地获取页面中某个元素的 CSS 选择器。

    左侧 web: 即抓取的页面的实时预览图。

    左侧 html: 抓取页面的 HTML 代码。

    左侧 follows: 如果当前抓取方法中又新建了爬取请求,那么接下来的请求就会出现在 follows 里。

    左侧 messages: 爬取过程中输出的一些信息。

    右侧代码区域: 你可以在右侧区域书写代码,并点击右上角的 Save 按钮保存。

    右侧 WebDAV Mode: 打开调试模式,左侧最大化,便于观察调试。

    #!/usr/bin/env python
    # -*- encoding: utf-8 -*-
    # Created on 2018-08-23 08:49:39
    # Project: pyspiderlianxi
    
    from pyspider.libs.base_handler import *
    import pymongo
    
    
    class Handler(BaseHandler):
        crawl_config = {
        }
        
        client = pymongo.MongoClient(host='localhost',port=27017)
        db = client['trip']
    
    
        @every(minutes=24 * 60)
        def on_start(self):
            self.crawl('https://www.tripadvisor.cn/Attractions-g294211-Activities-c47-China.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_info > div.listing_title > a').items(): self.crawl(each.attr.href, callback=self.detail_page,validate_cert=False) 爬取详情页# @config(priority=2) def detail_page(self, response): url = response.url, title = response.doc('title').text(), name = response.doc('#HEADING').text(), paiming = response.doc('span.header_rating > div > a > span').text(), phonenum = response.doc('div.blEntry.phone > span:nth-child(2)').text(), dizhi = response.doc('div.detail_section.address.xh-highlight').text(), youwanshijian = response.doc('#taplc_attraction_detail_listing_0 > div.section.hours > div').text() return { "url":url, "title":title, "name":name, "paiming":paiming, "phonenum":phonenum, "dizhi":dizhi, "youwanshijian":youwanshijian } #结果存入数据库中 def on_result(self,result): if result: self.save_to_mongo(result) def save_to_mongo(self,result): if self.db['chinastrip'].insert(result): print('save to mongo',result)

     

  • 相关阅读:
    WPF 文本动画 文字BaseLine 字体 行高计算说明
    C#查找窗口,并控制窗口显示隐藏,通过改变窗口样式方式
    c#获取状态栏图标并-模拟鼠标点击-模拟鼠标点击窗体的某些按钮
    IEC 104公约 解析 c#使用通过104公约同步时间
    hprose数据可视化显示,通过c#序列化,列表形式展示,导出excel、csv
    微服务优秀博文资料
    IT 常用单词表
    java 优秀开源项目
    java并发编程学习博客
    LDAP服务端
  • 原文地址:https://www.cnblogs.com/lmx123/p/9521683.html
Copyright © 2011-2022 走看看