Last active
October 11, 2018 06:20
-
-
Save ayu-mushi/adf0b96b81df657c4bfb050b6c467f5a to your computer and use it in GitHub Desktop.
首都取得capicity
This file contains hidden or 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
var request = require('request'); | |
url = 'https://query.wikidata.org/sparql' | |
var query="SELECT\ | |
?countriesLabel ?countriesDescription ?capitalLabel ?capitalDescription ?inception ?abolished ?dummy\ | |
WHERE {\ | |
{?countries rdfs:label \"" + process.argv[2] + | |
"\"@ja; wdt:P31 wd:Q3624078}\ | |
UNION {?countries skos:altLabel \"" + process.argv[2] + | |
"\"@ja; wdt:P31 wd:Q3624078}\ | |
UNION {?countries rdfs:label \"" + process.argv[2] + | |
"\"@en; wdt:P31 wd:Q3624078}\ | |
UNION {?countries skos:altLabel \"" + process.argv[2] + | |
"\"@en; wdt:P31 wd:Q3624078}\ | |
OPTIONAL { ?countries wdt:P36 ?capital }\ | |
OPTIONAL { ?countries wdt:P576 ?abolished }\ | |
OPTIONAL { ?countries wdt:P571 ?inception }\ | |
SERVICE wikibase:label {\ | |
bd:serviceParam wikibase:language 'ja,en'\ | |
}\ | |
}" | |
let params = { | |
format: "json" | |
,query: query | |
}; | |
var options = { | |
uri: url, | |
headers: { | |
"Content-type": "application/json", | |
}, | |
qs: params, | |
json: true | |
}; | |
var countries = []; | |
request.get(options, function(error, response, body){ | |
body.results.bindings.forEach(function(x){ | |
countries[x.countriesLabel.value] = { | |
capital : (x.capitalLabel ? x.capitalLabel.value : "none") | |
// , capitalDesc : (x.capitalDescription ? x.capitalDescription.value : "none") | |
, desc : (x.countriesDescription ? x.countriesDescription.value :"none") | |
, founded : (x.inception ? x.inception.value :"none") | |
, abolished : (x.abolished ? x.abolished.value :"none") | |
}; | |
}) | |
//console.log(body) | |
console.log(countries) | |
}); | |
This file contains hidden or 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
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
import urllib2 | |
from bs4 import BeautifulSoup | |
import sys | |
import os | |
import codecs | |
# 参考: https://qiita.com/Azunyan1111/items/b161b998790b1db2ff7a | |
#url = "https://ja.wikipedia.org/wiki/%E3%83%89%E3%82%A4%E3%83%84" | |
reload(sys) | |
sys.setdefaultencoding('utf8') | |
url = "https://ja.wikipedia.org/wiki/" + sys.argv[1] | |
instance = urllib2.urlopen(url) | |
soup = BeautifulSoup(instance, "html.parser") | |
result = soup.select_one(".infoboxCountryDataB > table:nth-of-type(1) > tbody:nth-of-type(1) > tr:nth-of-type(2) > td").text | |
print(result) | |
with codecs.open(os.environ['HOME'] + "/Dropbox/syuto.txt", 'a', 'utf-8') as f: | |
f = codecs.getwriter("utf-8")(f) | |
f.write("\n\n"+sys.argv[1] + ": " + result) |
This file contains hidden or 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
#!/bin/bash | |
ag $1 $HOME/Dropbox/syuto.txt |
This file contains hidden or 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
var request = require('request'); | |
url = 'https://query.wikidata.org/sparql' | |
var query="SELECT\ | |
?countriesLabel ?capitalLabel ?dummy\ | |
WHERE {\ | |
?countries wdt:P31 wd:Q3624078\ | |
OPTIONAL { ?countries wdt:P36 ?capital }\ | |
SERVICE wikibase:label {\ | |
bd:serviceParam wikibase:language 'ja,en'\ | |
}\ | |
}" | |
let params = { | |
format: "json" | |
,query: query | |
}; | |
var options = { | |
uri: url, | |
headers: { | |
"Content-type": "application/json", | |
}, | |
qs: params, | |
json: true | |
}; | |
var countries = []; | |
request.get(options, function(error, response, body){ | |
body.results.bindings.forEach(function(x){ | |
countries.push({ | |
name: x.countriesLabel.value | |
, capital : (x.capitalLabel ? x.capitalLabel.value : "none") | |
}); | |
}) | |
//console.log(body) | |
console.log(countries) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment