-
Notifications
You must be signed in to change notification settings - Fork 234
Scraping from Arabic news #2016
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
RabeaAffan24
wants to merge
6
commits into
data-for-change:dev
Choose a base branch
from
RabeaAffan24:dev
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
718d658
Scraping from PANET
ephraimberkovitch 2f90078
panet scraping + arabic classifier
ephraimberkovitch 5cc22c0
Merge branch 'hasadna:dev' into dev
RabeaAffan24 ed4bcb1
Merge branch 'hasadna:dev' into dev
RabeaAffan24 8c2da97
* Major refactoring of the code
RabeaAffan24 4855838
* Major refactoring of the Arabic scraping code
RabeaAffan24 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
|
|
||
| import requests | ||
| import re | ||
| import json | ||
| from datetime import datetime | ||
|
|
||
|
|
||
| class ArticleMetaData: | ||
| article_pub_date: datetime | ||
| abstract: str | ||
| title: str | ||
|
|
||
| def __init__(self, _article_pub_date, _abstract, _title): | ||
| self.article_pub_date = _article_pub_date | ||
| self.abstract = _abstract | ||
| self.title = _title | ||
|
|
||
|
|
||
| def scrape(panet_page): | ||
|
|
||
| website_content = requests.get(f"http://www.panet.co.il/category/home/2/{panet_page}") | ||
|
|
||
| """ Historic article titles: """ | ||
|
|
||
| regex1 = r"<div class=\"panet-title\">(.*?)<\/div>" | ||
| matches1 = re.finditer(regex1, website_content.text, re.MULTILINE) | ||
|
|
||
| with open(f"titles_{panet_page}.txt", "w", encoding="utf-8") as f1: | ||
|
|
||
| for matchNum, match in enumerate(matches1, start=1): | ||
|
|
||
| for groupNum in range(0, len(match.groups())): | ||
| groupNum = groupNum + 1 | ||
| f1.write(match.group(groupNum)) | ||
|
Comment on lines
32
to
34
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi,
|
||
| f1.write('\n') | ||
|
|
||
| """ Historic article abstracts: """ | ||
|
|
||
| regex2 = "<div class=\"panet-abstract\">(.*?)<(?:.*?)</div>" | ||
| matches2 = re.finditer(regex2, website_content.text, re.MULTILINE) | ||
|
|
||
| with open(f"abstracts_{panet_page}.txt", "w", encoding="utf-8") as f2: | ||
| for matchNum, match in enumerate(matches2, start=1): | ||
| f2.write(match.group(1)) | ||
| f2.write('\n') | ||
|
|
||
| """ Historic article pub time: """ | ||
|
|
||
| regex3 = r"<div class=\"panet-time-cont\".<time class=\"panet-time\">(.*?)<\/div>" | ||
| matches3 = re.finditer(regex3, website_content.text, re.MULTILINE) | ||
|
|
||
| with open(f"articles-pub-time_{panet_page}.txt", 'w', encoding='utf-8') as f3: | ||
| for matchNum, match in enumerate(matches3, start=1): | ||
|
|
||
| for groupNum in range(0, len(match.groups())): | ||
| groupNum = groupNum + 1 | ||
| f3.write(match.group(groupNum)) | ||
| f3.write('\n') | ||
|
|
||
| """ Code to scraping ABSTRACTS in a specified page range """ | ||
|
|
||
|
|
||
| def scrape_abstracts(): | ||
|
|
||
| for page in range(1, 5): | ||
| content = requests.get(f"http://www.panet.co.il/category/home/2/{page}/") | ||
| content_txt = content.text | ||
| with open(f"PanetPageText{page}.txt", "w", encoding='utf-8') as file_txt: | ||
| regex2 = r"<div class=\"panet-abstract\">(.*?)<(.*?)</div>" | ||
| matches2 = re.finditer(regex2, content_txt, re.MULTILINE) | ||
| for matchNum, match in enumerate(matches2, start=1): | ||
|
|
||
| for groupNum in range(0, len(match.groups())): | ||
| file_txt.write(match.group(1)) | ||
| file_txt.write('\n') | ||
|
|
||
|
|
||
| def generate_article_dict_and_classify(panet_page): | ||
|
|
||
| key_list = [line.strip('\n') for line in open(f"articles-pub-time_{panet_page}.txt", 'r', encoding='utf-8')] | ||
| value_list = [line.strip('\n') for line in open(f"abstracts_{panet_page}.txt", 'r', encoding='utf-8')] | ||
| # print(len(key_list)) | ||
| # print(len(value_list)) | ||
| assert len(key_list) == len(value_list) | ||
| article_dict = dict(zip(key_list, value_list)) | ||
| with open(f"article_dict_{panet_page}.txt", "w", encoding='utf-8') as f4: | ||
| f4.write(json.dumps(article_dict)) | ||
|
|
||
| key_keywords = ["حادث طرق", "حوادث طرق", "اصطدام", " دهس", "سائق", "سيارة", "انقلاب", "شاحنة", "مشاة"] | ||
| counter = 0 | ||
| for line in value_list: | ||
| if any([keyword in line for keyword in key_keywords]): | ||
| counter += 1 | ||
| with open(f"Car_Accident_News_Flash_Number_{counter}.txt", "w", encoding='utf-8') as CarAccidentNews: | ||
| CarAccidentNews.write(line.strip()) | ||
| CarAccidentNews.write('\n') | ||
|
|
||
|
|
||
| def unified_scrape(): | ||
|
|
||
| all_articles_dict = {} | ||
| website_content = requests.get("http://www.panet.co.il/category/home/2") | ||
| regex1 = r"<div class=\"panet-title\"><a href=\"/article/(.*?)\">(.*?)<\/a><\/div>" | ||
| matches1 = re.finditer(regex1, website_content.text, re.MULTILINE) | ||
| for matchNum, match in enumerate(matches1, start=1): | ||
| article_number = int(match.group(1)) | ||
| article_title = match.group(2) | ||
| all_articles_dict[article_number] = ArticleMetaData( | ||
| _article_pub_date=datetime.now(), | ||
| _title=article_title.strip(), | ||
| _abstract="" | ||
| ) | ||
|
|
||
| with open("articles.tsv", "w") as f: | ||
| for tsv in all_articles_dict.keys(): | ||
| val = all_articles_dict[tsv] | ||
| f.write(f"{tsv}\t{val.title}\t{val.article_pub_date}\n") | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| for panet_page in range(0, 301): | ||
| scrape(panet_page) | ||
| generate_article_dict_and_classify(panet_page) | ||
| # scrape_abstracts() | ||
| # unified_scrape() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, I would like us to use type hints in function parameters and variables.