scrapy从入门到放弃 学习项目4

news/2025/2/26 5:38:21

scrapy框架基于selenium,多页面爬取简书文章内容、作者,ajax技术传递的数据等

保存到MongoDB中

class JieshuxSpider(CrawlSpider):
    name = 'jieshux'
    allowed_domains = ['jianshu.com']
    start_urls = ['https://www.jianshu.com/']

    rules = (
        Rule(LinkExtractor(allow=r'.*/p/[a-z0-9]{12}.*'), callback='parse_item', follow=True),
    )

    def parse_item(self, response):
        title = response.xpath('//h1[@class="title"]/text()').get()
        content = response.xpath('//div[@class="show-content-free"]').get()
        author = response.xpath('//div[@class="info"]/span[@class="name"]/a/text()').get()
        publish_time = response.xpath('//div[@class="info"]/div/span[@class="publish-time"]/text()').get()\
            .replace('*', '')
        word_count = response.xpath('//div[@class="info"]/div/span[@class="wordage"]/text()').get().split(' ')[-1]
        read_count = response.xpath('//div[@class="info"]/div/span[@class="views-count"]/text()').get().split(' ')[-1]
        comment_count = response.xpath('//div[@class="info"]/div/span[@class="comments-count"]/text()').get().split(' ')[-1]
        like_count = response.xpath('//div[@class="info"]/div/span[@class="likes-count"]/text()').get().split(' ')[-1]
        subprocess = response.xpath('//div[@class="include-collection"]').get()
        art_id = response.url.split('?')[0].split('/p/')[-1]
        items = JieshuItem(title=title,
                           content=content,
                           art_id=art_id,
                           author=author,
                           publish_time=publish_time,
                           url=response.url,
                           word_count=word_count,
                           read_count=read_count,
                           comment_count=comment_count,
                           like_count=like_count,
                           subprocess=subprocess)
        yield items
jieshuspider
class JieshuSeleniumDownloaderMiddleware(object):

    def __init__(self):
        self.chromedriver = webdriver.Chrome(executable_path=CHROMEDRIVER_PATH)

    def process_request(self, request, spider):
        self.chromedriver.get(request.url)
        time.sleep(1)
        while 1:
            try:
                more = self.chromedriver.find_element_by_class_name('show_more')
                more.click()
                time.sleep(0.3)
            except:
                break
        source = self.chromedriver.page_source.encode('utf-8')
        response = HtmlResponse(url=self.chromedriver.current_url, body=source, request=request)
        return response
middleware
class JieshuPipeline(object):
    def __init__(self):
        self.db = pymongo.MongoClient('127.0.0.1:27017')

    def open_spider(self, spider):

        self.database = self.db['jianshu']
        self.table = self.database['jianshu']

    def process_item(self, item, spider):
        self.table.insert_one(dict(item))
        return item

    def close_spider(self, spider):
        self.db.close()
pipeline
class JieshuItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    title = scrapy.Field()
    content = scrapy.Field()
    art_id = scrapy.Field()
    author = scrapy.Field()
    publish_time = scrapy.Field()
    url = scrapy.Field()
    word_count = scrapy.Field()
    read_count = scrapy.Field()
    like_count = scrapy.Field()
    comment_count = scrapy.Field()
    subprocess = scrapy.Field()
items

利用下载器中间件方法process_request中返回response对象,跳过原本下载器对网页的下载。

利用selenium.Chrome()实现对ajax传递的信息的获取。

 

转载于:https://www.cnblogs.com/lpapython/p/11177378.html


http://www.niftyadmin.cn/n/4005711.html

相关文章

图解css3:核心技术与案例实战. 1.1 什么是CSS3

1.1 什么是CSS3 CSS3并不是一门新的语言。如果接触过CSS就知道,CSS是创建网页的另一个独立但并非不重要的一部分。CSS是种层叠样式表,是一种样式语言,用来告诉浏览器如何渲染你的Web页面。 CSS3是CSS规范的最新版本,在CSS2.1的基…

python基础知识四 小数据池,深浅拷贝,集合+菜中菜

四、小数据池,深浅拷贝,集合菜中菜 1小数据池 --缓存机制(驻留机制) ​ 判断两边内容是否相等 ​ is 基于内存地址进行判断是否相同 a 10 b 10 print(a b ) #is print(a is b)小数据池的数字范围: -5 ~256 a -5…

数据库系统(下)

文章目录并发控制数据库完整性约束数据库安全数据备份数据仓库与数据挖掘反规划化大数据并发控制 事务 存在的问题: 封锁协议&隔离级别 数据库完整性约束 目的是为了提高数据的可靠性。 实体完整性约束: 在使用数据库的时候,给数据表定…

Qt4.8.5安装超详细介绍

一、百度云下载三个软件 二、安装 1.首先安装qt creator,双击qt-creator-windows-opensource-2.8.0,注意安装目录不要有空格和特殊字符, 默认安装在C:\Qt 目录下。 2.安装mingw。qt框架可以使用VS提供的编译器,也可以使用GCC,在win下的版本就是mingw啦。其实也不是…

《BackTrack 5 Cookbook中文版——渗透测试实用技巧荟萃》—第1章1.2节安装BackTrack到硬盘...

本节书摘来自异步社区《BackTrack 5 Cookbook中文版——渗透测试实用技巧荟萃》一书中的第1章1.2节安装BackTrack到硬盘,作者【美】Pritchett Willie , 【墨】David De Smet,更多章节内容可以访问云栖社区“异步社区”公众号查看。 1.2 安装BackTrack到硬盘将BackT…

php构建型模式(Builder pattern)

练代码&#xff0c;增加了调用时的输出。 <?php /* The builder pattern separates the construction of a complex object from its representation, making it possible for the same construction process to create different representations. While some creational …

linux下xml安装和使用

xml安装和使用 需要文件&#xff1a;mxml-2.7.tar usemxml.c test.xml 步骤&#xff1a; 1.mxml-2.7.tar拷到共享文件夹 2.linux进入共享文件夹 3.#sudo su 4.#cp mxml-2.7.tar.gz /home/ 5.#tar -xvf mxml-2.7.tar.gz 6.#cd mxml-2.7 7.#./configure 8.make install…

计算机网络(上)

文章目录七层模型协议族网络类型和规划IP七层模型 OSI七层参考模型 协议族 基于TCP ICMP协议&#xff1a;路由协议ARP协议&#xff1a;地址解析&#xff08;ip转mac&#xff09;RARP协议&#xff1a;反向地址解析&#xff08;mac转ip&#xff09;TCP/UDP协议&#xff1a;TCP是…