Created
March 9, 2016 11:35
-
-
Save gh640/9a30783831e33c1c28e3 to your computer and use it in GitHub Desktop.
WordPress の人気プラグインの情報を複数件まとめて取得するスクリプト
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
# coding: utf-8 | |
""" | |
WordPress のプラグインリスト(プラグイン)の情報をまとめて取得する | |
usage: | |
$ python __file__ | |
""" | |
from pyquery import PyQuery as pq | |
URL = 'https://ja.wordpress.org/plugins/browse/popular/' | |
PAGE_MAX = 50 | |
def main(): | |
"""メイン | |
""" | |
main_single() | |
# main_multiple() | |
def main_multiple(): | |
"""メイン 複数件取得 | |
""" | |
for i in range(PAGE_MAX): | |
f = WordPressPluginFetcher(i) | |
f.extract_plugin_infos() | |
print(f.get_result()) | |
def main_single(): | |
"""メイン 1 件取得 | |
""" | |
f = WordPressPluginFetcher(0) | |
f.extract_plugin_infos() | |
print(f.get_result()) | |
class WordPressPluginFetcher(object): | |
"""WordPress のプラグインの情報を取得する | |
usage: | |
>>> f = WordPressPluginFetcher(2) | |
>>> f.extract_plugin_infos() | |
>>> print(f.get_result()) | |
""" | |
def __init__(self, page): | |
url = URL + 'page/' + str(page) if page != 0 else URL | |
self._q = pq(url) | |
self._plugins = [] | |
def extract_plugin_infos(self): | |
cards = self._q('.plugin-card') | |
self._plugins = list(map(self.extract_plugin_info, cards)) | |
def extract_plugin_info(self, d): | |
q = pq(d) | |
dom_a = q.find('.name a') | |
title = dom_a.text() | |
href = dom_a.attr('href') | |
desc = q.find('.desc').text() | |
return [title, href, desc] | |
def get_result(self): | |
output = ''; | |
for p in self._plugins: | |
output += ','.join(map(lambda x: '"{}"'.format(x), p)) + '\n' | |
return output | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment