一、安装

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

  1. pip install requests

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

  1. pip install beautifulsoup4

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

  1. pip install pymongo

4、安装mongo

二、分析网页&源代码

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

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

3、编写爬虫代码 并 执行

三、编写代码

  1. # -*- coding: utf-8 -*-
  2. # __author__ : "初一丶" 公众号:程序员共成长
  3. # __time__ : 2018/8/22 18:51
  4. # __file__ : spider_mayun.py
  5. # 导入相关库
  6. import requests
  7. from bs4 import BeautifulSoup
  8. import pymongo
  9.  
  10. """
  11. 通过分析页面url 查询不同语言的热门信息是有language这个参数决定的
  12. """
  13. # language = 'java'
  14. language = 'python'
  15. domain = 'https://gitee.com'
  16. uri = '/explore/starred?lang=%s' % language
  17. url = domain + uri
  18.  
  19. # 用户代理
  20. user_agent = 'Mozilla/5.0 (Macintosh;Intel Mac OS X 10_12_6) ' \
  21. 'AppleWebKit/537.36(KHTML, like Gecko) ' \
  22. 'Chrome/67.0.3396.99Safari/537.36'
  23.  
  24. # 构建header
  25. header = {'User_Agent': user_agent}
  26. # 获取页面源代码
  27. html = requests.get(url, headers=header).text
  28. # 获取Beautiful对象
  29. soup = BeautifulSoup(html)
  30.  
  31. # 热门类型分类 今日热门 本周热门 data-tab标签来区分当日热门和本周热门
  32. hot_type = ['today-trending', 'week-trending']
  33.  
  34. # divs = soup.find_all('div', class_='ui tab active')
  35. # 创建热门列表
  36. hot_gitee = []
  37. for i in hot_type:
  38. # 通过热门标签 查询该热门下的数据
  39. divs = soup.find_all('div', attrs={'data-tab': i})
  40. divs = divs[0].select('div.row')
  41. for div in divs:
  42. gitee = {}
  43. a_content = div.select('div.sixteen > h3 > a')
  44. div_content = div.select('div.project-desc')
  45. # 项目描述
  46. script = div_content[0].string
  47. # title属性
  48. title = a_content[0]['title']
  49. arr = title.split('/')
  50. # 作者名字
  51. author_name = arr[0]
  52. # 项目名字
  53. project_name = arr[1]
  54. # 项目url
  55. href = domain + a_content[0]['href']
  56. # 进入热门项目子页面
  57. child_page = requests.get(href, headers=header).text
  58. child_soup = BeautifulSoup(child_page)
  59. child_div = child_soup.find('div', class_='ui small secondary pointing menu')
  60. """
  61. <div class="ui small secondary pointing menu">
  62. <a class="item active" data-type="http" data-url="https://gitee.com/dlg_center/cms.git">HTTPS</a>
  63. <a class="item" data-type="ssh" data-url="git@gitee.com:dlg_center/cms.git">SSH</a>
  64. </div>
  65. """
  66. a_arr = child_div.findAll('a')
  67. # git http下载链接
  68. http_url = a_arr[0]['data-url']
  69. # git ssh下载链接
  70. ssh_url = a_arr[1]['data-url']
  71.  
  72. gitee['project_name'] = project_name
  73. gitee['author_name'] = author_name
  74. gitee['href'] = href
  75. gitee['script'] = script
  76. gitee['http_url'] = http_url
  77. gitee['ssh_url'] = ssh_url
  78. gitee['hot_type'] = i
  79.  
  80. # 连接mongo
  81. hot_gitee.append(gitee)
  82.  
  83. print(hot_gitee)
  84.  
  85. # 链接mongo参数
  86. HOST, PORT, DB, TABLE = '127.0.0.1', 27017, 'spider', 'gitee'
  87. # 创建链接
  88. client = pymongo.MongoClient(host=HOST, port=PORT)
  89. # 选定库
  90. db = client[DB]
  91. tables = db[TABLE]
  92. # 插入mongo库
  93. tables.insert_many(hot_gitee)

四、执行结果

  1. [{
  2. 'project_name': 'IncetOps',
  3. 'author_name': 'staugur',
  4. 'href': 'https://gitee.com/staugur/IncetOps',
  5. 'script': '基于Inception,一个审计、执行、回滚、统计sql的开源系统',
  6. 'http_url': 'https://gitee.com/staugur/IncetOps.git',
  7. 'ssh_url': 'git@gitee.com:staugur/IncetOps.git',
  8. 'hot_type': 'today-trending'
  9. }, {
  10. 'project_name': 'cms',
  11. 'author_name': 'dlg_center',
  12. 'href': 'https://gitee.com/dlg_center/cms',
  13. 'script': None,
  14. 'http_url': 'https://gitee.com/dlg_center/cms.git',
  15. 'ssh_url': 'git@gitee.com:dlg_center/cms.git',
  16. 'hot_type': 'today-trending'
  17. }, {
  18. 'project_name': 'WebsiteAccount',
  19. 'author_name': '张聪',
  20. 'href': 'https://gitee.com/crazy_zhangcong/WebsiteAccount',
  21. 'script': '各种问答平台账号注册',
  22. 'http_url': 'https://gitee.com/crazy_zhangcong/WebsiteAccount.git',
  23. 'ssh_url': 'git@gitee.com:crazy_zhangcong/WebsiteAccount.git',
  24. 'hot_type': 'today-trending'
  25. }, {
  26. 'project_name': 'chain',
  27. 'author_name': '何全',
  28. 'href': 'https://gitee.com/hequan2020/chain',
  29. 'script': 'linux 云主机 管理系统,包含 CMDB,webssh登录、命令执行、异步执行shell/python/yml等。持续更...',
  30. 'http_url': 'https://gitee.com/hequan2020/chain.git',
  31. 'ssh_url': 'git@gitee.com:hequan2020/chain.git',
  32. 'hot_type': 'today-trending'
  33. }, {
  34. 'project_name': 'Lepus',
  35. 'author_name': '茹憶。',
  36. 'href': 'https://gitee.com/ruzuojun/Lepus',
  37. 'script': '简洁、直观、强大的开源企业级数据库监控系统,MySQL/Oracle/MongoDB/Redis一站式监控,让数据库监控更简...',
  38. 'http_url': 'https://gitee.com/ruzuojun/Lepus.git',
  39. 'ssh_url': 'git@gitee.com:ruzuojun/Lepus.git',
  40. 'hot_type': 'today-trending'
  41. }, {
  42. 'project_name': 'AutoLink',
  43. 'author_name': '苦叶子',
  44. 'href': 'https://gitee.com/lym51/AutoLink',
  45. 'script': 'AutoLink是一个开源Web IDE自动化测试集成解决方案',
  46. 'http_url': 'https://gitee.com/lym51/AutoLink.git',
  47. 'ssh_url': 'git@gitee.com:lym51/AutoLink.git',
  48. 'hot_type': 'week-trending'
  49. }, {
  50. 'project_name': 'PornHubBot',
  51. 'author_name': 'xiyouMc',
  52. 'href': 'https://gitee.com/xiyouMc/pornhubbot',
  53. 'script': '全球最大成人网站PornHub爬虫 (Scrapy、MongoDB) 一天500w的数据',
  54. 'http_url': 'https://gitee.com/xiyouMc/pornhubbot.git',
  55. 'ssh_url': 'git@gitee.com:xiyouMc/pornhubbot.git',
  56. 'hot_type': 'week-trending'
  57. }, {
  58. 'project_name': 'wph_opc',
  59. 'author_name': '万屏汇',
  60. 'href': 'https://gitee.com/wph_it/wph_opc',
  61. 'script': None,
  62. 'http_url': 'https://gitee.com/wph_it/wph_opc.git',
  63. 'ssh_url': 'git@gitee.com:wph_it/wph_opc.git',
  64. 'hot_type': 'week-trending'
  65. }, {
  66. 'project_name': 'WebsiteAccount',
  67. 'author_name': '张聪',
  68. 'href': 'https://gitee.com/crazy_zhangcong/WebsiteAccount',
  69. 'script': '各种问答平台账号注册',
  70. 'http_url': 'https://gitee.com/crazy_zhangcong/WebsiteAccount.git',
  71. 'ssh_url': 'git@gitee.com:crazy_zhangcong/WebsiteAccount.git',
  72. 'hot_type': 'week-trending'
  73. }, {
  74. 'project_name': 'information27',
  75. 'author_name': '印妈妈',
  76. 'href': 'https://gitee.com/itcastyinqiaoyin/information27',
  77. 'script': None,
  78. 'http_url': 'https://gitee.com/itcastyinqiaoyin/information27.git',
  79. 'ssh_url': 'git@gitee.com:itcastyinqiaoyin/information27.git',
  80. 'hot_type': 'week-trending'
  81. }]

 

通过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. SQL 获取表结构

    select [表名]=c.Name, [表说明]=isnull(f.[value],''), [列序号]=a.Column_id, [列名]=a.Name, [列说明]=isnull(e.[valu ...

  2. 1.初识Node.js

    Node.js基础知识大汇总 1.下载并安装npm,检测安装是否成功(在命令行输入node -v,看是否会输出对应版本号) 2.写一个hello world 程序. (1).打开notepad,新建一 ...

  3. 超时导致的Galera节点加入集群失败

    需求:为galera集群添加新的节点. 初始化新的节点,加入的时候一直报错,加入失败,报错日志如下 WSREP_SST: [ERROR] Removing /var/lib/mysql//.sst/x ...

  4. MYSQL必知必会学习笔记

    8.1.1 百分号( %)通配符最常使用的通配符是百分号( %).在搜索串中, %表示任何字符出现任意次数.例如,为了找出所有以词jet起头的产品,可使用以下SELECT语句:SELECT prod_ ...

  5. jQuery倒计时组件(jquery.downCount.js)

    //html <span class="days">00</span> <span class="hours">00< ...

  6. java中List<Map<String, Object>>关于null的判断

    List<Map<String, Object>> selectTmFileInfo = fileInfoService.selectTmFileInfoByToken(cTo ...

  7. 对matplotlib库的运用

    1.matplotlib库的运用效果图 绘制基本的三角函数                                                                        ...

  8. windows 10 64位机器上 安装部署

    mi这个博客写的不错 https://www.cnblogs.com/dingguofeng/p/8709476.html 安装redis 可视化工具后 ,新建连接 名称随意,注意端口号是否有误默认6 ...

  9. hadoop基础操作

    通过hadoop上的hive完成WordCount 启动hadoop Hdfs上创建文件夹 上传文件至hdfs 启动Hive 创建原始文档表 导入文件内容到表docs并查看 用HQL进行词频统计,结果 ...

  10. 查看Python安装路径

    由于笔者自己经常忘记了如何查看Python的安装路径,又经常会用到Python的安装路径,因此记录一下,我们可以在命令行模式下输入: >>> import sys >>& ...