Skip to content

Instantly share code, notes, and snippets.

@mbdevpl
Created February 4, 2018 16:48
Show Gist options
  • Save mbdevpl/1d50248492b00257e2ef4733848413af to your computer and use it in GitHub Desktop.
Save mbdevpl/1d50248492b00257e2ef4733848413af to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import getpass"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"········\n"
]
}
],
"source": [
"api_key = getpass.getpass()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"········\n"
]
}
],
"source": [
"cse_id = getpass.getpass()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"from googleapiclient.discovery import build\n",
"import pprint"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"def google_search(search_term, api_key, cse_id, **kwargs):\n",
" service = build(\"customsearch\", \"v1\", developerKey=api_key)\n",
" res = service.cse().list(q=search_term, cx=cse_id, **kwargs).execute()\n",
" return res"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"results = google_search('stackoverflow site:en.wikipedia.org', api_key, cse_id, num=10)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"'https://en.wikipedia.org/wiki/Stack_overflow'\n",
"'https://en.wikipedia.org/wiki/Stack_overflow_(disambiguation)'\n",
"'https://en.wikipedia.org/wiki/Joel_Spolsky'\n",
"'https://en.wikipedia.org/wiki/Stack_buffer_overflow'\n",
"'https://en.wikipedia.org/wiki/Segmentation_fault'\n",
"'https://en.wikipedia.org/wiki/Fog_Creek_Software'\n",
"'https://en.wikipedia.org/wiki/Stack_Exchange'\n",
"'https://en.wikipedia.org/wiki/Jeff_Atwood'\n",
"'https://en.wikipedia.org/wiki/Buffer_overflow'\n",
"'https://en.wikipedia.org/wiki/Buffer_overflow_protection'\n"
]
}
],
"source": [
"for result in results['items']:\n",
" pprint.pprint(result['link'])\n",
" #print(i['title'] + \": \" + i['url'])\n",
"#pprint.pprint(results)"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [],
"source": [
"google_search_service = build(\"customsearch\", \"v1\", developerKey=api_key)"
]
},
{
"cell_type": "code",
"execution_count": 80,
"metadata": {},
"outputs": [],
"source": [
"results = google_search_service.cse().list(q='\"World Wide Fund for Nature\"', cx=cse_id).execute()"
]
},
{
"cell_type": "code",
"execution_count": 81,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'formattedSearchTime': '0.63',\n",
" 'formattedTotalResults': '142,000',\n",
" 'searchTime': 0.630174,\n",
" 'totalResults': '142000'}"
]
},
"execution_count": 81,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"results[\"searchInformation\"]"
]
},
{
"cell_type": "code",
"execution_count": 116,
"metadata": {},
"outputs": [],
"source": [
"results = google_search_service.cse().list(q='\"World Wide Fund for Nature\"', cx=cse_id,\n",
" cr='countryPL').execute()"
]
},
{
"cell_type": "code",
"execution_count": 117,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'formattedSearchTime': '0.53',\n",
" 'formattedTotalResults': '519',\n",
" 'searchTime': 0.53247,\n",
" 'totalResults': '519'}"
]
},
"execution_count": 117,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"results[\"searchInformation\"]"
]
},
{
"cell_type": "code",
"execution_count": 154,
"metadata": {},
"outputs": [],
"source": [
"result_counts = {}"
]
},
{
"cell_type": "code",
"execution_count": 157,
"metadata": {},
"outputs": [],
"source": [
"country_code = 'BG'\n",
"result_counts[country_code] = {}\n",
"for year in range(2000, 2015):\n",
" results = google_search_service.cse().list(q='\"World Wide Fund for Nature\"', cx=cse_id,\n",
" cr='country{}'.format(country_code), sort='date:r:19900101:{}0101'.format(year)).execute()\n",
" result_counts[country_code][year] = int(results[\"searchInformation\"]['totalResults'])"
]
},
{
"cell_type": "code",
"execution_count": 158,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'BG': {2000: 0,\n",
" 2001: 0,\n",
" 2002: 2,\n",
" 2003: 2,\n",
" 2004: 4,\n",
" 2005: 5,\n",
" 2006: 5,\n",
" 2007: 9,\n",
" 2008: 338,\n",
" 2009: 339,\n",
" 2010: 305,\n",
" 2011: 257,\n",
" 2012: 307,\n",
" 2013: 265,\n",
" 2014: 315},\n",
" 'PL': {2000: 0,\n",
" 2001: 0,\n",
" 2002: 4,\n",
" 2003: 5,\n",
" 2004: 7,\n",
" 2005: 8,\n",
" 2006: 10,\n",
" 2007: 441,\n",
" 2008: 440,\n",
" 2009: 438,\n",
" 2010: 499,\n",
" 2011: 499,\n",
" 2012: 495,\n",
" 2013: 492,\n",
" 2014: 409}}"
]
},
"execution_count": 158,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"result_counts"
]
},
{
"cell_type": "code",
"execution_count": 150,
"metadata": {},
"outputs": [],
"source": [
"results = google_search_service.cse().list(q='\"World Wide Fund for Nature\"', cx=cse_id,\n",
" cr='countryPL', sort='date:r:19900101:20000101').execute()"
]
},
{
"cell_type": "code",
"execution_count": 152,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'0'"
]
},
"execution_count": 152,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"int(results[\"searchInformation\"]['totalResults'])"
]
},
{
"cell_type": "code",
"execution_count": 148,
"metadata": {},
"outputs": [],
"source": [
"results = google_search_service.cse().list(q='\"World Wide Fund for Nature\" daterange:2447893-2451911', cx=cse_id,\n",
" cr='countryPL').execute()"
]
},
{
"cell_type": "code",
"execution_count": 149,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'formattedSearchTime': '0.20',\n",
" 'formattedTotalResults': '517',\n",
" 'searchTime': 0.201784,\n",
" 'totalResults': '517'}"
]
},
"execution_count": 149,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"results[\"searchInformation\"]"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [],
"source": [
"results = google_search('\"World Wide Fund for Nature\"', api_key, cse_id, num=10, cr='pl', hl='en')"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"results = google_search('\"World Wide Fund for Nature\"', api_key, cse_id, num=10, dateRestrict='???', cr='???')"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"import urllib.parse"
]
},
{
"cell_type": "code",
"execution_count": 75,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"'https://www.google.co.jp/search?q=\"World+Wide+Fund+for+Nature\"&lr=&cr=countryPL&hl=en&as_qdr=all&source=lnt&tbs=ctr:countryPL,cdr:1,cd_min:,cd_max:1/1/2014&tbm='"
]
},
"execution_count": 75,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"urllib.parse.unquote('https://www.google.co.jp/search?q=%22World+Wide+Fund+for+Nature%22&lr=&cr=countryPL&hl=en&as_qdr=all&source=lnt&tbs=ctr%3AcountryPL%2Ccdr%3A1%2Ccd_min%3A%2Ccd_max%3A1%2F1%2F2014&tbm=')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"'https://www.google.com/search?q=\"World+Wide+Fund+for+Nature\"&lr=&cr=countryPL&hl=en&as_qdr=all&tbs=&tbm='"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"cd_min:,cd_max:1/1/2014"
]
}
],
"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.6.0"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment