generated from worldbank/template
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
190 additions
and
155 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"id": "5476e3ee", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import os\n", | ||
"os.chdir(\"../../\")\n", | ||
"from dotenv import load_dotenv\n", | ||
"load_dotenv()\n", | ||
"\n", | ||
"import pandas as pd\n", | ||
"import numpy as np\n", | ||
"from src.google_trends import GT" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"id": "55d51a03", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"GoogleAPIkey = os.getenv(\"GoogleAPIkey\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"id": "9eecefe8", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"gt = GT(GoogleAPIkey)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 15, | ||
"id": "07aad8e2", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"{'item': [{'title': 'Fiji', 'mid': '/m/02wt0', 'value': 100},\n", | ||
" {'title': 'Fijian language', 'mid': '/m/020gps', 'value': 27},\n", | ||
" {'title': 'Concert tour', 'mid': '/g/122cngsd', 'value': 14},\n", | ||
" {'title': 'Island', 'mid': '/m/03s0c', 'value': 13},\n", | ||
" {'title': 'Package tour', 'mid': '/m/05x8w9', 'value': 11},\n", | ||
" {'title': 'Travel', 'mid': '/m/014dsx', 'value': 10},\n", | ||
" {'title': 'Resort', 'mid': '/m/02dkrm', 'value': 8},\n", | ||
" {'title': 'Australia', 'mid': '/m/0chghy', 'value': 7},\n", | ||
" {'title': 'Flight', 'mid': '/m/01515d', 'value': 7},\n", | ||
" {'title': 'New Zealand', 'mid': '/m/0ctw_b', 'value': 7},\n", | ||
" {'title': 'Nadi', 'mid': '/m/022wr3', 'value': 5},\n", | ||
" {'title': 'Holiday', 'mid': '/m/03gkl', 'value': 4},\n", | ||
" {'title': 'Cruise ship', 'mid': '/m/01tq2l', 'value': 4},\n", | ||
" {'title': 'Tourism', 'mid': '/g/120yrv6h', 'value': 4},\n", | ||
" {'title': 'Tour operator', 'mid': '/m/0477l6', 'value': 3},\n", | ||
" {'title': 'Sydney', 'mid': '/m/06y57', 'value': 3},\n", | ||
" {'title': 'Airport', 'mid': '/m/09cjl', 'value': 3},\n", | ||
" {'title': 'Concert', 'mid': '/m/01jddz', 'value': 3},\n", | ||
" {'title': 'Rugby union', 'mid': '/m/06br8', 'value': 3},\n", | ||
" {'title': 'Vacation', 'mid': '/m/02jwqh', 'value': 3},\n", | ||
" {'title': 'Beach', 'mid': '/m/0b3yr', 'value': 3},\n", | ||
" {'title': 'Location', 'mid': '/m/078x4m', 'value': 3},\n", | ||
" {'title': 'Travel visa', 'mid': '/m/01wtrk', 'value': 3},\n", | ||
" {'title': 'World Surf League', 'mid': '/m/089p0g', 'value': 2},\n", | ||
" {'title': 'Country', 'mid': '/m/01lff', 'value': 2}]}" | ||
] | ||
}, | ||
"execution_count": 15, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"gt.get_top_topics(\"Fiji Tour\", None)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "po", | ||
"language": "python", | ||
"name": "po" | ||
}, | ||
"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.9.0" | ||
}, | ||
"toc": { | ||
"base_numbering": 1, | ||
"nav_menu": {}, | ||
"number_sections": true, | ||
"sideBar": true, | ||
"skip_h1_title": false, | ||
"title_cell": "Table of Contents", | ||
"title_sidebar": "Contents", | ||
"toc_cell": false, | ||
"toc_position": {}, | ||
"toc_section_display": true, | ||
"toc_window_display": false | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,57 @@ | ||
import os | ||
os.chdir("../../") | ||
import json | ||
os.chdir("../../../") | ||
from src.scraper.scrape import * | ||
|
||
scraper = WebScraper( | ||
url= | ||
"https://www.theguardian.com/world/2023/aug/12/between-two-worlds-life-of-png-tribe-leader-and-plantation-owner-honoured", | ||
parser="xpath") | ||
scraper.load_page() | ||
scraper.parse_page() | ||
# Set up | ||
target_dir = os.getcwd() + "/data/text/rnz/" | ||
if not os.path.exists(target_dir): | ||
os.mkdir(target_dir) | ||
|
||
print(scraper.parsed_content) | ||
host_url = "https://www.rnz.co.nz" | ||
countries = ["Solomon Islands"] | ||
|
||
for country in countries: | ||
filename = country.replace(" ", "_").lower() + "_rnz_urls.csv" | ||
country_base_url = host_url + "/tags/" + str(country) + "?page=" | ||
country_urls = [country_base_url + str(i) for i in range(1, 500)] | ||
|
||
scraper = WebScraper(parser="html.parser") | ||
data = scraper.scrape_urls(country_urls, "o-digest__detail") | ||
|
||
output = [] | ||
for pg in data: | ||
for i in pg: | ||
output.append([i.find("h3").text, | ||
i.find("span").text, | ||
i.find("a")["href"]]) | ||
|
||
rnz_df = pd.DataFrame( | ||
output, columns=["title", "date", "url"]).drop_duplicates() | ||
rnz_df["news"] = rnz_df["url"].apply( | ||
lambda x: x.split("/")[2] != "programmes") | ||
rnz_df["url"] = [host_url + str(url) for url in rnz_df.url] | ||
|
||
# Save url files | ||
rnz_df.to_csv(target_dir + filename, encoding="utf-8") | ||
|
||
for country in countries: | ||
country_filepath = target_dir + country.lower().replace(" ", "_") + \ | ||
"_rnz_urls.csv" | ||
df = pd.read_csv(country_filepath).drop("Unnamed: 0", axis=1) | ||
news_urls = df[df.news == True]["url"].tolist() | ||
scraper = WebScraper(parser="html.parser") | ||
nested_data = scraper.scrape_urls(news_urls, "article__body", speed_up=True) | ||
|
||
news_output = [] | ||
for url, i in nested_data: | ||
try: | ||
text = "".join(p.text for p in i[0].find_all("p")) | ||
news_output.append([url, text]) | ||
except Exception as e: | ||
print(f"An Error has occured: {e}.") | ||
news_output.append([url, np.NAN]) | ||
|
||
country_news_df = pd.DataFrame(news_output, columns=["url", "news"]) | ||
news_filepath = target_dir + country.lower().replace(" ", "_") + \ | ||
"_rnz_news.csv" | ||
country_news_df.to_csv(news_filepath, encoding="utf-8") |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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