一、安装

1、通过requests 对响应内容进行处理,requests.get()方法会返回一个Response对象

pip install requests

2、beautifulSoup对网页解析不仅灵活、高效而且非常方便,支持多种解析器

pip install beautifulsoup4

3、pymongo是python操作mongo的工具包

pip install pymongo

4、安装mongo

二、分析网页&源代码

1、确定目标:首先要知道要抓取哪个页面的哪个版块

2、分析目标:确定抓取目标之后要分析URL链接格式以及拼接参数的含义其次还要分析页面源代码确定数据格式

3、编写爬虫代码 并 执行

三、编写代码

# -*- coding: utf-8 -*-
# __author__ : "初一丶" 公众号:程序员共成长
# __time__ : 2018/8/22 18:51
# __file__ : spider_mayun.py
# 导入相关库
import requests
from bs4 import BeautifulSoup
import pymongo """
通过分析页面url 查询不同语言的热门信息是有language这个参数决定的
"""
# language = 'java'
language = 'python'
domain = 'https://gitee.com'
uri = '/explore/starred?lang=%s' % language
url = domain + uri # 用户代理
user_agent = 'Mozilla/5.0 (Macintosh;Intel Mac OS X 10_12_6) ' \
'AppleWebKit/537.36(KHTML, like Gecko) ' \
'Chrome/67.0.3396.99Safari/537.36' # 构建header
header = {'User_Agent': user_agent}
# 获取页面源代码
html = requests.get(url, headers=header).text
# 获取Beautiful对象
soup = BeautifulSoup(html) # 热门类型分类 今日热门 本周热门 data-tab标签来区分当日热门和本周热门
hot_type = ['today-trending', 'week-trending'] # divs = soup.find_all('div', class_='ui tab active')
# 创建热门列表
hot_gitee = []
for i in hot_type:
# 通过热门标签 查询该热门下的数据
divs = soup.find_all('div', attrs={'data-tab': i})
divs = divs[0].select('div.row')
for div in divs:
gitee = {}
a_content = div.select('div.sixteen > h3 > a')
div_content = div.select('div.project-desc')
# 项目描述
script = div_content[0].string
# title属性
title = a_content[0]['title']
arr = title.split('/')
# 作者名字
author_name = arr[0]
# 项目名字
project_name = arr[1]
# 项目url
href = domain + a_content[0]['href']
# 进入热门项目子页面
child_page = requests.get(href, headers=header).text
child_soup = BeautifulSoup(child_page)
child_div = child_soup.find('div', class_='ui small secondary pointing menu')
"""
<div class="ui small secondary pointing menu">
<a class="item active" data-type="http" data-url="https://gitee.com/dlg_center/cms.git">HTTPS</a>
<a class="item" data-type="ssh" data-url="git@gitee.com:dlg_center/cms.git">SSH</a>
</div>
"""
a_arr = child_div.findAll('a')
# git http下载链接
http_url = a_arr[0]['data-url']
# git ssh下载链接
ssh_url = a_arr[1]['data-url'] gitee['project_name'] = project_name
gitee['author_name'] = author_name
gitee['href'] = href
gitee['script'] = script
gitee['http_url'] = http_url
gitee['ssh_url'] = ssh_url
gitee['hot_type'] = i # 连接mongo
hot_gitee.append(gitee) print(hot_gitee) # 链接mongo参数
HOST, PORT, DB, TABLE = '127.0.0.1', 27017, 'spider', 'gitee'
# 创建链接
client = pymongo.MongoClient(host=HOST, port=PORT)
# 选定库
db = client[DB]
tables = db[TABLE]
# 插入mongo库
tables.insert_many(hot_gitee)

四、执行结果

[{
'project_name': 'IncetOps',
'author_name': 'staugur',
'href': 'https://gitee.com/staugur/IncetOps',
'script': '基于Inception,一个审计、执行、回滚、统计sql的开源系统',
'http_url': 'https://gitee.com/staugur/IncetOps.git',
'ssh_url': 'git@gitee.com:staugur/IncetOps.git',
'hot_type': 'today-trending'
}, {
'project_name': 'cms',
'author_name': 'dlg_center',
'href': 'https://gitee.com/dlg_center/cms',
'script': None,
'http_url': 'https://gitee.com/dlg_center/cms.git',
'ssh_url': 'git@gitee.com:dlg_center/cms.git',
'hot_type': 'today-trending'
}, {
'project_name': 'WebsiteAccount',
'author_name': '张聪',
'href': 'https://gitee.com/crazy_zhangcong/WebsiteAccount',
'script': '各种问答平台账号注册',
'http_url': 'https://gitee.com/crazy_zhangcong/WebsiteAccount.git',
'ssh_url': 'git@gitee.com:crazy_zhangcong/WebsiteAccount.git',
'hot_type': 'today-trending'
}, {
'project_name': 'chain',
'author_name': '何全',
'href': 'https://gitee.com/hequan2020/chain',
'script': 'linux 云主机 管理系统,包含 CMDB,webssh登录、命令执行、异步执行shell/python/yml等。持续更...',
'http_url': 'https://gitee.com/hequan2020/chain.git',
'ssh_url': 'git@gitee.com:hequan2020/chain.git',
'hot_type': 'today-trending'
}, {
'project_name': 'Lepus',
'author_name': '茹憶。',
'href': 'https://gitee.com/ruzuojun/Lepus',
'script': '简洁、直观、强大的开源企业级数据库监控系统,MySQL/Oracle/MongoDB/Redis一站式监控,让数据库监控更简...',
'http_url': 'https://gitee.com/ruzuojun/Lepus.git',
'ssh_url': 'git@gitee.com:ruzuojun/Lepus.git',
'hot_type': 'today-trending'
}, {
'project_name': 'AutoLink',
'author_name': '苦叶子',
'href': 'https://gitee.com/lym51/AutoLink',
'script': 'AutoLink是一个开源Web IDE自动化测试集成解决方案',
'http_url': 'https://gitee.com/lym51/AutoLink.git',
'ssh_url': 'git@gitee.com:lym51/AutoLink.git',
'hot_type': 'week-trending'
}, {
'project_name': 'PornHubBot',
'author_name': 'xiyouMc',
'href': 'https://gitee.com/xiyouMc/pornhubbot',
'script': '全球最大成人网站PornHub爬虫 (Scrapy、MongoDB) 一天500w的数据',
'http_url': 'https://gitee.com/xiyouMc/pornhubbot.git',
'ssh_url': 'git@gitee.com:xiyouMc/pornhubbot.git',
'hot_type': 'week-trending'
}, {
'project_name': 'wph_opc',
'author_name': '万屏汇',
'href': 'https://gitee.com/wph_it/wph_opc',
'script': None,
'http_url': 'https://gitee.com/wph_it/wph_opc.git',
'ssh_url': 'git@gitee.com:wph_it/wph_opc.git',
'hot_type': 'week-trending'
}, {
'project_name': 'WebsiteAccount',
'author_name': '张聪',
'href': 'https://gitee.com/crazy_zhangcong/WebsiteAccount',
'script': '各种问答平台账号注册',
'http_url': 'https://gitee.com/crazy_zhangcong/WebsiteAccount.git',
'ssh_url': 'git@gitee.com:crazy_zhangcong/WebsiteAccount.git',
'hot_type': 'week-trending'
}, {
'project_name': 'information27',
'author_name': '印妈妈',
'href': 'https://gitee.com/itcastyinqiaoyin/information27',
'script': None,
'http_url': 'https://gitee.com/itcastyinqiaoyin/information27.git',
'ssh_url': 'git@gitee.com:itcastyinqiaoyin/information27.git',
'hot_type': 'week-trending'
}]

 

通过Python、BeautifulSoup爬取Gitee热门开源项目的更多相关文章

  1. [原创]python+beautifulsoup爬取整个网站的仓库列表与仓库详情

    from bs4 import BeautifulSoup import requests import os def getdepotdetailcontent(title,url):#爬取每个仓库 ...

  2. Python使用urllib,urllib3,requests库+beautifulsoup爬取网页

    Python使用urllib/urllib3/requests库+beautifulsoup爬取网页 urllib urllib3 requests 笔者在爬取时遇到的问题 1.结果不全 2.'抓取失 ...

  3. PYTHON 爬虫笔记九:利用Ajax+正则表达式+BeautifulSoup爬取今日头条街拍图集(实战项目二)

    利用Ajax+正则表达式+BeautifulSoup爬取今日头条街拍图集 目标站点分析 今日头条这类的网站制作,从数据形式,CSS样式都是通过数据接口的样式来决定的,所以它的抓取方法和其他网页的抓取方 ...

  4. python大规模爬取京东

    python大规模爬取京东 主要工具 scrapy BeautifulSoup requests 分析步骤 打开京东首页,输入裤子将会看到页面跳转到了这里,这就是我们要分析的起点 我们可以看到这个页面 ...

  5. Python+Selenium爬取动态加载页面(2)

    注: 上一篇<Python+Selenium爬取动态加载页面(1)>讲了基本地如何获取动态页面的数据,这里再讲一个稍微复杂一点的数据获取全国水雨情网.数据的获取过程跟人手动获取过程类似,所 ...

  6. Python+Selenium爬取动态加载页面(1)

    注: 最近有一小任务,需要收集水质和水雨信息,找了两个网站:国家地表水水质自动监测实时数据发布系统和全国水雨情网.由于这两个网站的数据都是动态加载出来的,所以我用了Selenium来完成我的数据获取. ...

  7. 用Python爬虫爬取广州大学教务系统的成绩(内网访问)

    用Python爬虫爬取广州大学教务系统的成绩(内网访问) 在进行爬取前,首先要了解: 1.什么是CSS选择器? 每一条css样式定义由两部分组成,形式如下: [code] 选择器{样式} [/code ...

  8. python之爬取网页数据总结(一)

    今天尝试使用python,爬取网页数据.因为python是新安装好的,所以要正常运行爬取数据的代码需要提前安装插件.分别为requests    Beautifulsoup4   lxml  三个插件 ...

  9. 大神:python怎么爬取js的页面

    大神:python怎么爬取js的页面 可以试试抓包看看它请求了哪些东西, 很多时候可以绕过网页直接请求后面的API 实在不行就上 selenium (selenium大法好) selenium和pha ...

随机推荐

  1. 小程序即时聊天(仅页面功能,未接websocket)

    我们都知道小程序是不能直接操作dom的,所以做即时通讯聊天功能的时候也就不能像之前做的一样:点击发送的时候,拼接一个节点到页面上.话不多说,以下是效果和代码: -------------------- ...

  2. 从Facebook数据泄露事件看大数据时代的个人信息安全问题

    进入21世纪后,互联网开始大规模普及,线上业务和线上服务也开始逐渐走入人们的生活.尤其在智能手机和移动互联网诞生以后,人们对网络的依赖更是与日俱增.然而,伴随而来的则是涉及个人隐私的信息安全问题.个人 ...

  3. 51单片机创建PDF文件

    PDF文件有特定的格式要求,本以为.TXT与.PDF之间可以相互转换,只需要修改后缀名就可以了,然而事实并非如此. 如下为.PDF文件打开的编码显示. 如果需要创建PDF文件,只需要按照PDF的编码格 ...

  4. 勾勾街:用最小的成本封装一个苹果IOS APP! 封装技术再度升级~

    勾勾街自上线以来,“遭到”大量群众的喜爱... 只能用遭到这个词儿,因为大家好像都被憋了很久了,哈哈哈! 我们的技术是先进的,也是首创的,但最近发现了另一个网站,把我们的技术抄走了.... 本来这个事 ...

  5. Android滑动列表(拖拽,左滑删除,右滑完成)功能实现(1)

    场景: 近期做的TODO APP需要在主页添加一个功能,就是可以左滑删除,右滑完成.看了一下当前其他人做的例如仿探探式的效果,核心功能基本一样,但是和我预想的还是有少量区别,于是干脆自己重头学一遍如何 ...

  6. redux 与 react-redux

    Redux 一.Redux 三大原则: 1.一个应用永远只有一个数据源(整个应用状态都保存在一个对象中,Redux提供的工具函数combineReducers可以解决庞大的数据对象的问题) 2.状态是 ...

  7. Matlab调用遗传工具箱复现论文模型求解部分

    原文转载自:https://blog.csdn.net/robert_chen1988/article/details/52431594 论文来源: https://www.sciencedirect ...

  8. mysql学习3

    1.索引 索引是表的目录,在查找内容之前可以先在目录中查找索引位置,以此快速定位查询数据.对于索引, 会保存在额外的文件中. 作用: 约束 加速查找 1.1.建立索引 a.额外的文件保存特殊的数据结构 ...

  9. Tips_信息列表(手风琴)效果的多种实现方法

    效果图: 一.纯CSS实现 <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...

  10. WPF实现只打开一个窗口,并且重复打开时已经打开的窗口置顶

    内容来自:https://codereview.stackexchange.com/questions/20871/single-instance-wpf-application 第一步:添加Syst ...