记录:一次爬取gitee项目名称和url[通俗易懂]

记录:一次爬取gitee项目名称和url[通俗易懂]items:classGiteeItem(scrapy.Item):link=scrapy.Field()desc=scrapy.Field()passdb

items:

class GiteeItem(scrapy.Item):
    link = scrapy.Field()
    desc = scrapy.Field()
    pass

db:

import emoji
import pymysql

connect = pymysql.connect(host='localhost', user='root', password='root', db='mindsa', charset='utf8mb4')
cursor = connect.cursor()


def insertGitee(item):
    sql = """INSERT INTO gitee(link,`desc`) VALUES ({},{})""".format("'" + emoji.demojize(item['link']) + "'",
                                                                     "'" + emoji.demojize(item['desc']) + "'")
    cursor.execute(sql)
    connect.commit()

pipelines:

class GiteePipeline:
    def process_item(self, item, spider):
        insertGitee(item)

settings:

ITEM_PIPELINES = {
    'myscrapy.pipelines.GiteePipeline': 300,
}
GiteeSprider:
import scrapy

from myscrapy.items import GiteeItem


class GiteeSprider(scrapy.Spider):
    name = 'gitee'
    allow_domains = 'gitee.com'
    start_urls = ['https://gitee.com/explore/all']

    def parse(self, response, **kwargs):
        # 使用绝对路径定位标签
        elements = response.xpath('//div[@class="ui relaxed divided items explore-repo__list"]//div[@class="item"]')
        for element in elements:
            # 注意:再次进行xpath的时候是相对路径在需要//前面加上.。是.//而不是//
            link = self.allow_domains + element.xpath('.//h3/a/@href').get()
            desc = element.xpath('.//div[@class="project-desc"]/text()').get()
            item = GiteeItem()
            item['link'] = link
            item['desc'] = desc
            yield item
        # 注意:根据多个属性值进行xpath的时候,用and来连接。
        next_href__get = response.xpath(
            '//div[@class="ui tiny pagination menu"]//a[@class="icon item" and @rel="next"]/@href'
        ).get()

        if next_href__get is not None:
            # 如果存在下一页则继续请求
            yield scrapy.Request("https://gitee.com"+next_href__get, self.parse)

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/219206.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • FVWM简明使用指南 pdf_fpv教程

    FVWM简明使用指南 pdf_fpv教程http://docs.huihoo.com/homepage/shredderyin/fvwm.html FVWM简明使用指南FvwmisawindowmanagerforX11.Itisdesignedtominimizememoryconsumption,providea3Dlooktowindowframes,andavirtual

    2022年10月3日
  • 什么时候市场出现真正拐点

    什么时候市场出现真正拐点 从《实盘日志》中披露的数据我们能够发现,目前股指已经被大资金牢牢掌握,话语权已经交给了大盘股,这一点从昨天的银行股群体上涨就能够看出问题。 《实盘日志》贴出的图表显示,昨天银行板块资金净流入就高达9个亿,但是,8日资金净流出较大的个股有紫金矿业、…

    2022年6月18日
  • log4j2 debug_log4j原理

    log4j2 debug_log4j原理长话短说吧。相信大家已经被Log4j2的重大漏洞刷屏了,估计有不少小伙伴此前为了修bug已经累趴下了。很不幸,我的小老弟小二的SpringBoot项目中恰好用的就是Log4j2,版本特喵的还是2.14.1,在这次漏洞波及的版本范围之内。第一时间从网上得知这个漏洞的消息后,小二吓尿了。赶紧跑过来问老王怎么解决。老王先是给小二提供了一些临时性的建议,比如说:JVM参数添加-Dlog4j2.formatMsgNoLookups=truelog4j2.formatMsgNoLooku

    2022年10月29日
  • CSS文字大小单位px、em、pt(转)

    CSS文字大小单位px、em、pt(转)

    2021年8月27日
  • C#-TextBox-登录窗口密码不可见—ShinePans[通俗易懂]

    C#-TextBox-登录窗口密码不可见—ShinePans[通俗易懂]usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespaceTextB

    2022年7月18日
  • java.io.outputstream_java input

    java.io.outputstream_java inputio流概述:IO流用来处理设备之间的数据传输,上传文件和下载文件,Java对数据的操作是通过流的方式,Java用于操作流的对象都在IO包中。IO流分类按照数据流向输入流读入数据输出流写出数据按照数据类型字节流字符流什么情况下使用哪种流呢?如果数据所在的文件通过windows自带的记事本打开并能读懂里面的内容,就用字符流,其他用字节流。如果你什么都…

    2022年9月21日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号