Update Prothom_alo_fully_scraped.py
Browse files- Prothom_alo_fully_scraped.py +78 -78
Prothom_alo_fully_scraped.py
CHANGED
@@ -1,79 +1,79 @@
|
|
1 |
-
def get_data(number):
|
2 |
-
print("Running Prothom_alo_fully_scraped")
|
3 |
-
##Necessary imports
|
4 |
-
from deep_translator import GoogleTranslator
|
5 |
-
from selenium import webdriver
|
6 |
-
from selenium.webdriver import chrome
|
7 |
-
from selenium.webdriver import ChromeOptions
|
8 |
-
from datetime import datetime, timedelta
|
9 |
-
import re
|
10 |
-
#PROXY = "45.251.231.113:5678"
|
11 |
-
options = ChromeOptions()
|
12 |
-
#options.add_argument('--proxy-server=%s' % PROXY)
|
13 |
-
|
14 |
-
driver = webdriver.Chrome(options=options)
|
15 |
-
## Finding Elements by XPATH
|
16 |
-
from selenium.webdriver.common.by import By
|
17 |
-
import time, math
|
18 |
-
driver.get("https://www.prothomalo.com/topic/%E0%A6%B8%E0%A7%9C%E0%A6%95-%E0%A6%A6%E0%A7%81%E0%A6%B0%E0%A7%8D%E0%A6%98%E0%A6%9F%E0%A6%A8%E0%A6%BE")
|
19 |
-
time.sleep(15)
|
20 |
-
news_list=[]
|
21 |
-
news_link=[]
|
22 |
-
publish_date=[]
|
23 |
-
if number<=15:
|
24 |
-
txt=driver.find_elements(By.CLASS_NAME, "title-link")
|
25 |
-
date=driver.find_elements(By.TAG_NAME, "time")
|
26 |
-
for i in range(number):
|
27 |
-
news_list.append(txt[i].text)
|
28 |
-
news_link.append(txt[i].get_attribute("href"))
|
29 |
-
publish_date.append(date[i].text)
|
30 |
-
|
31 |
-
else:
|
32 |
-
clck=int((number-25)/15 + 2)
|
33 |
-
for i in range(clck):
|
34 |
-
print(i)
|
35 |
-
time.sleep(10)
|
36 |
-
last_height = driver.execute_script("return document.body.scrollHeight")
|
37 |
-
driver.execute_script(f"window.scrollTo(0, {last_height-1050})")
|
38 |
-
button=driver.find_elements(By.CLASS_NAME, "tNj8k")
|
39 |
-
button[0].click()
|
40 |
-
time.sleep(5)
|
41 |
-
txt=driver.find_elements(By.CLASS_NAME, "title-link")
|
42 |
-
date=driver.find_elements(By.TAG_NAME, "time")
|
43 |
-
for i in range(number):
|
44 |
-
news_list.append(txt[i].text)
|
45 |
-
news_link.append(txt[i].get_attribute("href"))
|
46 |
-
publish_date.append(date[i].text)
|
47 |
-
|
48 |
-
###### Scraping Description modified for translation######
|
49 |
-
from deep_translator import GoogleTranslator
|
50 |
-
from goose3 import Goose
|
51 |
-
from datetime import datetime
|
52 |
-
g = Goose()
|
53 |
-
description=[]
|
54 |
-
News_title=[]
|
55 |
-
publish_date=[]
|
56 |
-
for i in range(len(news_link)):
|
57 |
-
print(i)
|
58 |
-
article = g.extract(url=news_link[i])
|
59 |
-
### Only for Prothom Alo ###
|
60 |
-
# Access the articleBody field
|
61 |
-
data = article.schema
|
62 |
-
article_body = data.get('articleBody')
|
63 |
-
print("Para length", len(article_body))
|
64 |
-
if(len(article_body)>=2200):
|
65 |
-
article_body=article_body[0:2200]
|
66 |
-
bangla_title=article.title
|
67 |
-
english_title = GoogleTranslator(source='auto', target='en').translate(text=bangla_title)
|
68 |
-
News_title.append(english_title)
|
69 |
-
text = GoogleTranslator(source='auto', target='en').translate(text=article_body)
|
70 |
-
description.append(text)
|
71 |
-
publish_date.append(article.publish_date)
|
72 |
-
# Convert the dates to "day-month-year" format
|
73 |
-
formatted_dates = [datetime.fromisoformat(date).strftime('%d-%m-%Y') for date in publish_date]
|
74 |
-
|
75 |
-
#### Converting the list to a pandas dataframe by converting the list to a dictionary ###
|
76 |
-
dict={'News Title':News_title,'News Link':news_link,'Publish Date':formatted_dates, 'Description':description}
|
77 |
-
import pandas as pd
|
78 |
-
df=pd.DataFrame(dict)
|
79 |
return df
|
|
|
1 |
+
def get_data(number):
|
2 |
+
print("Running Prothom_alo_fully_scraped")
|
3 |
+
##Necessary imports
|
4 |
+
from deep_translator import GoogleTranslator
|
5 |
+
from selenium import webdriver
|
6 |
+
from selenium.webdriver import chrome
|
7 |
+
from selenium.webdriver import ChromeOptions
|
8 |
+
from datetime import datetime, timedelta
|
9 |
+
import re
|
10 |
+
#PROXY = "45.251.231.113:5678"
|
11 |
+
options = ChromeOptions()
|
12 |
+
#options.add_argument('--proxy-server=%s' % PROXY)
|
13 |
+
options.add_argument("--headless=new")
|
14 |
+
driver = webdriver.Chrome(options=options)
|
15 |
+
## Finding Elements by XPATH
|
16 |
+
from selenium.webdriver.common.by import By
|
17 |
+
import time, math
|
18 |
+
driver.get("https://www.prothomalo.com/topic/%E0%A6%B8%E0%A7%9C%E0%A6%95-%E0%A6%A6%E0%A7%81%E0%A6%B0%E0%A7%8D%E0%A6%98%E0%A6%9F%E0%A6%A8%E0%A6%BE")
|
19 |
+
time.sleep(15)
|
20 |
+
news_list=[]
|
21 |
+
news_link=[]
|
22 |
+
publish_date=[]
|
23 |
+
if number<=15:
|
24 |
+
txt=driver.find_elements(By.CLASS_NAME, "title-link")
|
25 |
+
date=driver.find_elements(By.TAG_NAME, "time")
|
26 |
+
for i in range(number):
|
27 |
+
news_list.append(txt[i].text)
|
28 |
+
news_link.append(txt[i].get_attribute("href"))
|
29 |
+
publish_date.append(date[i].text)
|
30 |
+
|
31 |
+
else:
|
32 |
+
clck=int((number-25)/15 + 2)
|
33 |
+
for i in range(clck):
|
34 |
+
print(i)
|
35 |
+
time.sleep(10)
|
36 |
+
last_height = driver.execute_script("return document.body.scrollHeight")
|
37 |
+
driver.execute_script(f"window.scrollTo(0, {last_height-1050})")
|
38 |
+
button=driver.find_elements(By.CLASS_NAME, "tNj8k")
|
39 |
+
button[0].click()
|
40 |
+
time.sleep(5)
|
41 |
+
txt=driver.find_elements(By.CLASS_NAME, "title-link")
|
42 |
+
date=driver.find_elements(By.TAG_NAME, "time")
|
43 |
+
for i in range(number):
|
44 |
+
news_list.append(txt[i].text)
|
45 |
+
news_link.append(txt[i].get_attribute("href"))
|
46 |
+
publish_date.append(date[i].text)
|
47 |
+
|
48 |
+
###### Scraping Description modified for translation######
|
49 |
+
from deep_translator import GoogleTranslator
|
50 |
+
from goose3 import Goose
|
51 |
+
from datetime import datetime
|
52 |
+
g = Goose()
|
53 |
+
description=[]
|
54 |
+
News_title=[]
|
55 |
+
publish_date=[]
|
56 |
+
for i in range(len(news_link)):
|
57 |
+
print(i)
|
58 |
+
article = g.extract(url=news_link[i])
|
59 |
+
### Only for Prothom Alo ###
|
60 |
+
# Access the articleBody field
|
61 |
+
data = article.schema
|
62 |
+
article_body = data.get('articleBody')
|
63 |
+
print("Para length", len(article_body))
|
64 |
+
if(len(article_body)>=2200):
|
65 |
+
article_body=article_body[0:2200]
|
66 |
+
bangla_title=article.title
|
67 |
+
english_title = GoogleTranslator(source='auto', target='en').translate(text=bangla_title)
|
68 |
+
News_title.append(english_title)
|
69 |
+
text = GoogleTranslator(source='auto', target='en').translate(text=article_body)
|
70 |
+
description.append(text)
|
71 |
+
publish_date.append(article.publish_date)
|
72 |
+
# Convert the dates to "day-month-year" format
|
73 |
+
formatted_dates = [datetime.fromisoformat(date).strftime('%d-%m-%Y') for date in publish_date]
|
74 |
+
|
75 |
+
#### Converting the list to a pandas dataframe by converting the list to a dictionary ###
|
76 |
+
dict={'News Title':News_title,'News Link':news_link,'Publish Date':formatted_dates, 'Description':description}
|
77 |
+
import pandas as pd
|
78 |
+
df=pd.DataFrame(dict)
|
79 |
return df
|