Pyspider的使用

作者: 原来不语 | 来源:发表于2018-02-11 08:46 被阅读0次
from pyspider.libs.base_handler import *
import pymongo
class Handler(BaseHandler):
crawl_config = {
}
client = pymongo.MongoClient('localhost')
db = client['trip']
@every(minutes=24 * 60)
def on_start(self):
    self.crawl('https://www.tripadvisor.cn/Attractions-g34345-Activities-c47-Key_West_Florida_Keys_Florida.html', callback=self.index_page, validate_cert=False)

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

@config(priority=2)
def detail_page(self, response):
    name = response.doc('.shelf_row_1 .name > a').text()
    address=response.doc('#taplc_attraction_detail_listing_0 > div.section.location').text()
    phone = response.doc('#taplc_attraction_detail_listing_0 > div.section.contact.last > div.ui_columns > div.ui_column.phone > div').text()
    
    return {
        "name":name,
        "url": response.url,
        "title": response.doc('title').text(),
        "address":address,
        "phone":phone
    }

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('saved to mongo', result)

pyspider用pyquery来获取元素
在抓取时如果报下边错误

HTTP 599: SSL certificate problem: self signed certificate in certificate chain

只需要使用这个即可
self.crawl(url, callback=self.index_page, validate_cert=False)
该实例是抓取https://www.tripadvisor.cn的一些信息的实例,并且把抓取的信息存储到Mongodb数据库中

相关文章

  • pyspider 爬取 去哪网 游记内容 图片

    昨天学习了pyspider的使用《Python 3 网络爬虫开发实战》中介绍了使用pyspider爬取去哪的游记内...

  • pyspider web爬虫框架的使用

    一、为什么要使用pyspider? 我们很好奇,我们明明有了Scrapy框架,为什么还要使用pyspider框架呢...

  • Pyspider基本使用

    pyspider web爬虫框架简单使用 pip3 install pyspider 在桌面创建一个pyspide...

  • pyspider使用

    一个国人编写的强大的网络爬虫系统并带有强大的WebUI。采用Python语言编写,分布式架构,支持多种数据库后端,...

  • Pyspider的使用

    pyspider用pyquery来获取元素在抓取时如果报下边错误 HTTP 599: SSL certificat...

  • Python pyspider的安装错误

    在安装pyspider的时候出现 因为pycurl 的ssl导致的错误 pyspider的使用需要用到pycurl...

  • pyspider 爬虫教程

    注:采转归档,自己学习查询使用 pyspider 爬虫教程(1):HTML 和 CSS 选择器pyspider 爬...

  • manjaro安装pyspider

    在安装pyspider时出现各种问题,这里记录一下。 直接使用pip install pyspider,在安装py...

  • Mac python3.7环境 安装 pyspider 排坑

    正常操作安装pip3 install pyspider 然后使用pyspider all 然后遇到第一个问题Fil...

  • 红海战役与战狼2影评分析

    一、数据爬取 使用pyspider,在豆瓣把两部电影的影评拉下来,存储起来 pyspider脚本: 需要在/usr...

网友评论

    本文标题:Pyspider的使用

    本文链接:https://www.haomeiwen.com/subject/lgbhtftx.html