简书网站 爬取所有文章(同步方式保存数据库)

 

import scrapy
import re
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule

from jianshu.items import JianshuItem


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

    rules = (
        # 匹配文章链接
        Rule(LinkExtractor(allow=r'.*/p/[0-9a-z]{12}.*'), callback='parse_detail', follow=True),
    )

    def parse_detail(self, response):
        title = response.xpath('//h1[@class="title"]/text()').get()
        avatar = response.xpath('//a[@class="avatar"]/img/@src').get()
        author = response.xpath('//span[@class="name"]//text()').get()
        content = response.xpath('//div[@class="show-content"]').get()
        pub_time = response.xpath('//span[@class="publish-time"]/text()').get().replace('*', '')
        read_count = response.xpath('//span[@class="views-count"]/text()').get()
        comment_count = response.xpath('//span[@class="comments-count"]/text()').get()
        like_count = response.xpath('//span[@class="likes-count"]/text()').get()
        rewards_count = response.xpath('//span[@class="wordage"]/text()').get()
        print('-------------', rewards_count)
        # 获取数字
        read_count = re.findall('\d+', read_count)[0] if re.findall('\d+', read_count) else None
        comment_count = re.findall('\d+', comment_count)[0] if re.findall('\d+', comment_count) else None
        like_count = re.findall('\d+', like_count)[0] if re.findall('\d+', like_count) else None
        rewards_count = re.findall('\d+', rewards_count)[0] if re.findall('\d+', rewards_count) else None
        item = JianshuItem(
            title=title,
            avatar=avatar,
            author=author,
            content=content,
            pub_time=pub_time,
            read_count=read_count,
            comment_count=comment_count,
            like_count=like_count,
            rewards_count=rewards_count
        )
        yield item

 

items:

import scrapy


class JianshuItem(scrapy.Item):
    title = scrapy.Field()
    avatar = scrapy.Field()
    author = scrapy.Field()
    content = scrapy.Field()
    pub_time = scrapy.Field()
    read_count = scrapy.Field()
    comment_count = scrapy.Field()
    like_count = scrapy.Field()
    rewards_count = scrapy.Field()

 

 

 

pipline:

class JianShuPipeline(object):
    def __init__(self):
        db_params = {
            'host': '127.0.0.1',
            'port': 3306,
            'user': 'root',
            'password': '',
            'database': 'jianshu',
            'charset': 'utf8'
        }
        self.conn = pymysql.connect(**db_params)
        self.cursor = self.conn.cursor()
        self._sql = None

    def process_item(self, item, spider):
        self.cursor.execute(self.sql,
                            (item['title'], item['content'], item['author'], item['avatar'], item['pub_time'],
                             item['like_count'], item['read_count'], item['comment_count'], item['rewards_count']))
        self.conn.commit()
        return item

    @property
    def sql(self):
        if not self._sql:
            self._sql = """
           INSERT INTO article(id, title, content, author, avatar, pub_time, like_count, read_count, comment_count, 
           rewards_count) VALUES (null, %s, %s, %s, %s, %s, %s, %s, %s, %s);
           """
            return self._sql
        return self._sql

 

转载于:https://www.cnblogs.com/yuqiangli0616/p/10338756.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值