A python library for parsing news articles
Project description
Malaysian News Parser
The Malaysian News Parser, is a python library for extracting and parsing news articles from multiple publishers. It supports both static and dynamic content parsing using BeautifulSoup and Selenium. This parser was developed with a focus on Malaysian news publishers, but this may work with other news organizations provided it is allowed.
Table of Contents
Installation
You can install the library using pip
pip install parser_dev
Usage
Initializing the Config Manager
Before using the library, you need to initialize the `PublisherConfigManager`:
This can be done by importing the manager from `publisher_configs`, and then initializing an instance of the manager
from parser_dev.publisher_config import PublisherConfigManager # Import Manager
# Initialize Manager instance
config_manager = PublisherConfigManager()
This was set as a global variable in the usage tests
Adding a Publisher
There are slight format differences between setting the publisher configuration for static and dynamic sites.
Static Publisher Config Format:
static_publisher_config = {
'type': 'static', # For static, set this to static
'headers': { # Only present for website types that are static. Omitted for Dynamic
'header_name': 'header_value'
},
'body_selector': { # Article Body Selector info for beautiful soup
'type': 'class, id, css'
'element': '<div>, <article>, etc..., not required for css selector',
'text': 'selector_text'
},
'date_selector': { # Article Date Selector info for beautiful soup
'type': 'class, id, css',
'element': '<div>, <article>, etc..., not required for css selector',
'text': 'selector_text'
},
'author_selector': { # Article Author Selector info for beautiful soup
'type': 'class, id, css',
'element': '<div>, <article>, etc..., not required for css selector',
'text': 'selector_text'
}
}
Dynamic Publisher Config Format:
# Headers omitted, chrome_path and sleep_time added.
dynamic_publisher_config = {
'type': 'dynamic', # For dynamic, set this to dynamic
'chrome_path': os.getenv('CHROME_PATH') # your path to your Selenium compatible webdriver (set in your .env file)
'body_selector': { # Article Body Selector info for beautiful soup
'type': 'class, id, css'
'element': '<div>, <article>, etc..., not required for css selector',
'text': 'selector_text'
},
'date_selector': { # Article Date Selector info for beautiful soup
'type': 'class, id, css',
'element': '<div>, <article>, etc..., not required for css selector',
'text': 'selector_text'
},
'author_selector': { # Article Author Selector info for beautiful soup
'type': 'class, id, css',
'element': '<div>, <article>, etc..., not required for css selector',
'text': 'selector_text'
},
'sleep_time': 10, # set how long the webdriver should wait to render page, dtype: int
}
To add a new publisher configuration:
from parser_dev.publisher_config import PublisherConfigManager # Import Manager
# Initialize Manager instance
config_manager = PublisherConfigManager()
# desired new publisher configuration
new_publisher_config = {
'type': 'static',
'headers': {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
'Accept-Language': 'en-US,en;q=0.9',
'Accept-Encoding': 'gzip, deflate, br',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Connection': 'keep-alive',
'Upgrade-Insecure-Requests': '1',
},
'body_selector': {
'type': 'css',
'element': 'div',
'text': '#new-body-selector',
},
'date_selector': {
'type': 'css',
'element': 'div',
'text': '#new-date-selector',
}
}
config_manager.add_publisher('new_publisher', new_publisher_config)
Updating a Publisher
To update an existing publisher configuration:
update_publisher_config = {
'body_selector': {
'type': 'css',
'text': 'body > main > section > div > div > div > section > div:nth-of-type(1) > strong',
}
}
config_manager.update_publisher('new_publisher', update_publisher_config)
Remove a Publisher
To remove a publisher configuration:
config_manager.remove_publisher('new_publisher')
Retrieving Article Data
The existing publisher config names
| Publisher | Name in Config |
|---|---|
| Astro Awani | astro_awani |
| Malay Mail | malay_mail |
| Star | star |
| Sun Daily | sun_daily |
| FMT (Free Malaysia Today) | fmt |
| Sinar Harian | sinar_harian |
| Berita Harian | berita_harian |
| Roti Kaya | roti_kaya |
| New Straits Times | nst |
| The Vibes | the_vibes |
| World of Buzz | world_of_buzz |
| Bernama | bernama |
| Cili Sos | cili_sos |
| Coconuts | coconuts |
| The Edge | the_edge |
| HMetro | hmetro |
| Malaysian Gazette | malaysia_gazette |
| Harakah Daily | harakah_daily |
| Borneo Post | borneo_post |
To retrieve article data from a given URL:
from parser_dev import get_parser
# url of article
url = 'https://www.astroawani.com/berita-malaysia/pelajar-cemerlang-spm-dijamin-tempat-matrikulasi-atau-program-kpt-kpm-477101'
# publisher name
publisher_name = 'astro_awani'
# Get appropriate parser
parser = get_parser(publisher_name)
# Extract and parse article data
article_data = parser.get_article_data(url)
if article_data:
print(f"Title: {article_data['title']}")
print(f"Author: {article_data['author']}")
print(f"Date: {article_data['date']}")
print(f"Body: {article_data['body'][:1000]}") # Print the first 1000 characters to keep it concise
else:
print("Failed to fetch or parse article data.")
Testing
To run the tests, navigate to the project directory and use `unittest`:
python -m unittest discover -s src/tests
Contributing
We welcome contributions! Please follow these steps:
- Fork the Repository
- Create a new branch: `git checkout -b feature/your-feature-name
- Make your changes and commit them: `git commit -m 'Add some feature'
- Push to the branch: `git push origin feature/your-feature-name`
- Submit a pull request.
Please make sure to update tests as appropriate.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file malaysian_news_parser-0.1.4.tar.gz.
File metadata
- Download URL: malaysian_news_parser-0.1.4.tar.gz
- Upload date:
- Size: 23.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0c10fc5132237b658f565ea2f6f67c376ead9ee933830a6dff785502c12d13a8
|
|
| MD5 |
502646d398f98081a5373c147e984bf3
|
|
| BLAKE2b-256 |
216da353b6d3a592f814518b4700e14c80e26bc09c04d68c77ba5848f7c57966
|
File details
Details for the file malaysian_news_parser-0.1.4-py3-none-any.whl.
File metadata
- Download URL: malaysian_news_parser-0.1.4-py3-none-any.whl
- Upload date:
- Size: 27.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
85c5737e719d96d2435c07faa80d835ca98e7a5dc30f3946c456ce146d275164
|
|
| MD5 |
781ff40029599b26dcac1960a8def6d1
|
|
| BLAKE2b-256 |
9ea207fe7c89af2404727b6b87572d80686c3987eaa5208e39e694a8e7f95460
|