Created
March 31, 2019 10:12
-
-
Save incidunt/f31b2ed67005423555b0c59e265df2c8 to your computer and use it in GitHub Desktop.
获取WordPress插件的icon地址
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"获取WordPress插件的icon地址\n", | |
"实际效果页面:[WP精品插件大全](https://bestscreenshot.com/best-wordpress-plugins/)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import json\n", | |
"import requests" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def get_vaild_icon(slug):\n", | |
" \n", | |
" icon_url=''\n", | |
" \n", | |
" icon_urls= [ 'https://ps.w.org/{0}/assets/{1}'.format(slug, i) \n", | |
" for i in ['icon.svg', 'icon-128x128.png', 'icon-128x128.jpg', 'icon-256x256.png', 'icon-256x256.jpg' ] \n", | |
" ]\n", | |
" \n", | |
" icon_default='https://s.w.org/plugins/geopattern-icon/{}.svg'.format(slug)\n", | |
" \n", | |
" for url in icon_urls:\n", | |
" icon_request= requests.get(url)\n", | |
" if icon_request.status_code==200:\n", | |
" icon_url=url\n", | |
" break\n", | |
" else:\n", | |
" icon_url=icon_default\n", | |
" \n", | |
" return icon_url" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"with open('data_filtered_result.json') as json_file:\n", | |
" filtered_plugins = json.load(json_file)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{'slug': 'accelerated-mobile-pages',\n", | |
" 'added': '2016-02-07',\n", | |
" 'author': '<a href=\"https://ampforwp.com/\">Ahmed Kaludi, Mohammed Kaludi</a>',\n", | |
" 'downloaded': 4137343,\n", | |
" 'last_updated': '2019-02-28 12:52pm GMT',\n", | |
" 'name': 'AMP for WP – Accelerated Mobile Pages',\n", | |
" 'num_ratings': 780,\n", | |
" 'rating': 90,\n", | |
" 'support_threads': 286,\n", | |
" 'support_threads_resolved': 60,\n", | |
" 'tested': '5.1',\n", | |
" 'last_updated_days': 30}" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"filtered_plugins[1]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'https://ps.w.org/accelerated-mobile-pages/assets/icon-128x128.png'" | |
] | |
}, | |
"execution_count": 8, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"get_vaild_icon('accelerated-mobile-pages')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"for plugin in filtered_plugins:\n", | |
" slug = plugin['slug']\n", | |
" plugin['icon'] = get_vaild_icon(slug)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# write back with icon url\n", | |
"json.dump(filtered_plugins, open(\"data_filtered_result.json\",\"w\"))\n" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.7.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment