Wr 自留地 Wr 自留地
  • 首页
  • 资源
  • 工具
  • 教程
  • 标签
  • 友链
  • 关于

使用 Python 爬取 WordPress.org 上所有插件json信息

_Wr_ 3年前
Python 爬虫

准备

  • Python 环境
  • requests 模块
  • BeautifulSoup 4 模块

前言

最近在做 WP-China-Yes 插件商城的项目,其中就需要用 Python 爬 WordPress.org 上所有可翻译的项目。尽管还没找到如何筛选可翻译的项目...

教程

首先我们要知道,WordPress.org 所有插件的 slug (永久链接) 都可以通过 http://plugins.svn.wordpress.org/ 得到:

使用 Python 爬取 WordPress.org 上所有插件json信息-Wr 自留地
import requests
#通过 requests 模块获取 http://plugins.svn.wordpress.org 这个网页上的内容
html=requests.get("http://plugins.svn.wordpress.org/").text

接着,我们又需要知道 WordPress.org 的所有插件的 json 信息都可以通过 https://api.wordpress.org/plugins/info/1.0/永久链接.json 获取

Python 爬虫第二步:获取 json 文件
from bs4 import BeautifulSoup
#使用 BeautifulSoup 获取 WordPress.org 插件 json 内容
soup=BeautifulSoup(html,features="lxml")
lis=soup.find_all('li')
baseurl="https://api.wordpress.org/plugins/info/1.0/"

最后一步,就需要输出内容了。但总不可能直接用 print() 函数输出吧,要输出成一个 txt 文件

with open('all_plugins_urls.txt','a') as out:
    for a in soup.find_all('a', href=True):
        out.write(baseurl+a['href'].replace('/','')+".json"+"\n")

综上所述,整个 Python 爬虫代码应该是这个样子的

import requests 
from bs4 import BeautifulSoup

html=requests.get("http://plugins.svn.wordpress.org/").text

soup=BeautifulSoup(html,features="lxml")
lis=soup.find_all('li')
baseurl="https://api.wordpress.org/plugins/info/1.0/"

with open('all_plugins_urls.txt','a') as out:
    for a in soup.find_all('a', href=True):
        out.write(baseurl+a['href'].replace('/','')+".json"+"\n")

你瞧瞧这代码,是多么的精简呐。看来人生苦短,我用Python这句话讲的多么的对啊~

PythonWordPress插件教程编程
相关文章
  • [教程] 腾讯云服务器 Ubuntu 实例开启 root 用户通过密码 SSH 远程登录教程
  • [教程]《索尼克大冒险 2》PC 版 Mod 制作教程 (一): 各文件/目录的详细作用
  • Xiuno 付费插件购买分享
  • BAT 脚本仿蓝屏
  • WordPress教程:查看当天用户注册数量以及用户注册时间排序
_Wr_「作者」
赞赏
_Wr_
评论 (1)
发表评论
Copyright © 2019-2023 Wr 自留地.        
  • 首页
  • 资源
  • 工具
  • 教程
  • 标签
  • 友链
  • 关于