当前位置: 首页 > news >正文

爬虫学习日记第七篇(爬取github搜索仓库接口,其实不算爬虫)

github提供的搜索仓库的API https://api.github.com/

# 连接数据库
db = mysql.connector.connect(host="***",user="***",password="***",database="***"
)
# 创建游标
cursor = db.cursor()
# 从数据库中读取CVE ID
cursor.execute("SELECT cve_id FROM vules WHERE cve_id != '无CVE' AND poc != '暂无可利用代码'")
cve_ids = cursor.fetchall()
print(cve_ids)# 遍历CVE ID列表
for cve_id in cve_ids:cve_id = cve_id[0]  # 提取CVE ID值# 在GitHub上搜索CVE IDURL = f'https://api.github.com/search/repositories?q={cve_id}&sort=stars'r = requests.get(URL)response_dict = r.json()print(response_dict)repo_dicts = response_dict['items']results = []for i in range(len(repo_dicts)):results.append(repo_dicts[i]["html_url"])print(results)
# 关闭数据库连接
db.close()

报错,限制了API访问速率
{‘message’: “API rate limit exceeded for ******. (But here’s the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)”, ‘documentation_url’: ‘https://docs.github.com/rest/overview/resources-in-the-rest-api#rate-limiting’}

需要添加Authentication认证
在github个人主页setting/ Developer settings/personal access token/generate new token,把生成的token复制保存下来

headers = {'User-Agent':'Mozilla/5.0','Authorization': 'token ef802a122df2e4d29d9b1b868a6fefb14f22b272',    //填写拿到的token'Content-Type':'application/json','Accept':'application/json'}

加上token之后速率好了一些,但还是又报错了

{‘message’: ‘API rate limit exceeded for user ID ******. If you reach out to GitHub Support for help, please include the request ID FCA0:25083D:2521AF:27C7D1:6528B0F7.’, ‘documentation_url’: ‘https://docs.github.com/rest/overview/resources-in-the-rest-api#rate-limiting’}

用不了多线程,遂只能加上sleep慢慢读取,和try except。

    try:print(cve_id)cve_id = cve_id[0]  # 提取CVE ID值# 在GitHub上搜索CVE IDURL = f'https://api.github.com/search/repositories?q={cve_id}&sort=stars'r = requests.get(URL,headers=headers)response_dict = r.json()print(response_dict)repo_dicts = response_dict['items']results = []for i in range(len(repo_dicts)):results.append(repo_dicts[i]["html_url"])result = ','.join(results)sql = "UPDATE vules SET repositories=%s WHERE cve_id=%s;"values = (result, cve_id)cursor.execute(sql, values)db.commit()print(results)sleep(1)except Exception as e:# 捕获到异常后的处理代码# 打印异常信息print("发生异常:", str(e))# 等待几秒后继续执行循环sleep(5)continue
http://www.lryc.cn/news/194855.html

相关文章:

  • 子组件监听父组件消息,随之变化与不变化
  • 计算机操作系统面试题自用
  • redis作为消息队列的缺点
  • Redis五大数据类型的底层设计
  • logback的简单配置详解
  • TatukGIS Developer Kernel使用教程:如何为FMX创建第一个应用程序
  • Ant Design Vue设置表格滚动 宽度自适应 不换行
  • 在Linux上开启文件服务,需要安装并配置Samba
  • TypeScript 类型兼容性
  • 【多线程】线程的状态
  • pytorch 对图片进行归一化处理
  • 零售数据分析师熬夜整理:人、货、场、供、财这样做
  • 基于SSM的学生选课管理系统
  • SQL注入漏洞
  • C++ wpf自制软件打包安装更新源码实例
  • 8月19日PMP成绩,预计10月16日公布!附查询入口、流程
  • 简易LDO设计(包含原理图、PCB和实验)
  • SpringBoot面试题5:SpringBoot Starter的工作原理是什么?
  • Leetcode 2902. Count of Sub-Multisets With Bounded Sum
  • ARP协议(地址解析协议) 的作用和操作过程
  • 轻游戏风格虚拟资源付费下载模板Discuz论坛模板
  • MongoDB索引操作
  • AMEYA360:君正低功耗AIoT图像识别处理器—X1600/X1600E
  • EM@圆和圆锥曲线的参数方程
  • uniapp 微信小程序 vue3.0+TS手写自定义封装步骤条(setup)
  • Python 金融大数据分析
  • 初识C++入门(1)
  • 使用Selenium的WebDriver进行长截图
  • python+大数据校园卡数据分析 计算机竞赛
  • 【机器学习】sklearn降维算法PCA