To run this tool, you need to have Python 3.11 installed in your system.
Open up a terminal window, navigate to this repository and run this command:
make install
This tool is used to scrape Google News articles based on the topic they're listed in.
First of all, open up Google News, and look through the topics listed in the top header of the webpage.
![image](https://private-user-images.githubusercontent.com/44357929/345088740-c30be402-90cb-41ba-95a4-b2f63cdf5a7f.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQ3NjgwOTYsIm5iZiI6MTcyNDc2Nzc5NiwicGF0aCI6Ii80NDM1NzkyOS8zNDUwODg3NDAtYzMwYmU0MDItOTBjYi00MWJhLTk1YTQtYjJmNjNjZGY1YTdmLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjclMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODI3VDE0MDk1NlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWQ5YzdlY2NjNGE2NWQwMjhkNzE2NmQ2NTQ5MzU4MGRmYjgzZDBmZjU4NzUyN2Q0YjMxM2FkY2ZmNjdmOGJlNDgmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.dAV9NezYk-81KopJMASPEhM1l8LO2F6ZdL2fQ98Kmnw)
Click on a topic you wish to scrape.
In this example we'll be using the Business
topic.
Next, look at the URL in your browser and copy the string of characters that come after /topics/
, that's your topic ID.
![image](https://private-user-images.githubusercontent.com/44357929/345090644-cc3efc01-e719-4497-80f9-45710cb47977.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQ3NjgwOTYsIm5iZiI6MTcyNDc2Nzc5NiwicGF0aCI6Ii80NDM1NzkyOS8zNDUwOTA2NDQtY2MzZWZjMDEtZTcxOS00NDk3LTgwZjktNDU3MTBjYjQ3OTc3LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjclMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODI3VDE0MDk1NlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTM5OTk5OTBiY2VhMDVlMTA2NTg0ZjZkMTQzNThjNzcyM2QyM2ZiMmVhNTY1MmFjOWU1OWJiNGFmZWY1NGIyY2ImWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.XpovIU7_Astzk0tA06P6IXluZJQitALVi9aXH3sk-OM)
In the URL shown in the screenshot, the topic ID would be CAAqJggKIiBDQkFTRWdvSUwyMHZNRGx6TVdZU0FtVnVHZ0pWVXlnQVAB
.
Save this value, you'll need it for scraping the articles.
To scrape articles from your selected topic, run this command in your terminal:
make scrape TOPIC_ID=<your_selected_topic_id>
With the Business
topic ID selected before, the command should look like this:
make scrape TOPIC_ID=CAAqJggKIiBDQkFTRWdvSUwyMHZNRGx6TVdZU0FtVnVHZ0pWVXlnQVAB
After running the command, you should see this in your terminal:
![image](https://private-user-images.githubusercontent.com/44357929/345092982-ad3e8211-043d-40fe-87e3-89fc9fea2381.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQ3NjgwOTYsIm5iZiI6MTcyNDc2Nzc5NiwicGF0aCI6Ii80NDM1NzkyOS8zNDUwOTI5ODItYWQzZTgyMTEtMDQzZC00MGZlLTg3ZTMtODlmYzlmZWEyMzgxLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjclMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODI3VDE0MDk1NlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWYxZWE0NjU2M2I4Mjg3Y2UxYzI4NjU2MzI1NTA3ZmEyMWY3MzAyMjg1NjA3YzA1ZjQ4NGQyMmQ2NmJjMjI4NDQmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.UR6VnUOMfOSIuH5p9oq8D2uN50at5yMDD8MaT7UvNkY)
When the tool has finished running, you should see a file named articles.csv
in the directory you were running the tool.
If you open the generated CSV file, the data should look something like this:
![image](https://private-user-images.githubusercontent.com/44357929/345093565-7127198e-1aa9-468d-a6e6-b989a68085f3.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQ3NjgwOTYsIm5iZiI6MTcyNDc2Nzc5NiwicGF0aCI6Ii80NDM1NzkyOS8zNDUwOTM1NjUtNzEyNzE5OGUtMWFhOS00NjhkLWE2ZTYtYjk4OWE2ODA4NWYzLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjclMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODI3VDE0MDk1NlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTNiMDgxNDIwYjBmNTg0YzdhZDE1MmIxMjk2YjI0ZTViNTcwOWEwZjk0MjAxZGE2NTM1MTQwZDQ3OWIxYWMwNzcmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.J9pv83sVi1iAfAqFOTiwVXzX8esH-enYTFyvwp_3G2o)
In case the code doesn't work or your project is of bigger scale, please refer to the second part of the tutorial. There, we showcase how to scrape public data with Oxylabs Scraper API.
You can get a 7-day trial for Oxylabs Google News API and get free 5K results. The tool will deliver a list of sources, titles, URLs, and dates from published articles all over the Google News portal. This API returns real-time data and gives access to localized results, all while avoiding blocks.
After you claim your trial, using Google News API consists of three main steps:
- Create your API user via our dashboard
- Send a request
- Retrieve the data in JSON or HTML
In the example below, we use Google News API and make a request to collect search result pages for the search term adidas
on the google.nl
domain:
import requests
from pprint import pprint
# Structure payload.
payload = {
'source': 'google_search',
'domain': 'nl',`
'query': 'adidas',
'parse': True,
'context': [
{'key': 'tbm', 'value': 'nws'},
],
}
# Get response.
response = requests.post(
'https://realtime.oxylabs.io/v1/queries',
auth=('USERNAME', 'PASSWORD'),
json=payload,
)
# Print prettified response to stdout.
pprint(response.json())
To see request samples in other languages and parameter values along with their descriptions, please take a look at our extensive Google News API documentation.