-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrape_to_csv.py
More file actions
58 lines (46 loc) · 1.6 KB
/
scrape_to_csv.py
File metadata and controls
58 lines (46 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import re, string, json, time
import pandas as pd
import numpy as np
from pathlib import Path
from progress.spinner import *
import crayons
import requests, json, bs4
from progress.bar import *
import pandas as pd
from urllib.parse import ParseResult, urlparse
df_test = pd.read_csv("data/orig/FakeNewsNet.csv")
df_test.drop_duplicates(inplace = True)
df_test.dropna(inplace = True)
#df_test = shuffle(df_test)
df = []
set_N = 100
status = {
'success_real': 0,
'success_fake': 0,
'failed': 0,
'tested': 0
}
with PixelBar('Scraping page...', max=set_N) as bar:
for idx, row in df_test.sample(n=set_N).iterrows():
bar.bar_prefix = 'Scraping "{}"...'.format(str(row["title"])[:15])
bar.update()
p = urlparse(row["news_url"], 'https')
netloc = p.netloc or p.path
path = p.path if p.netloc else ''
if not netloc.startswith('www.'):
netloc = 'www.' + netloc
fixed_url = ParseResult('https', netloc, path, *p[3:]).geturl()
try:
response = requests.get(
fixed_url, headers={'User-Agent': 'Mozilla/5.0', 'cache-control': 'max-age=0'}, cookies={'cookies': ''})
soup = bs4.BeautifulSoup(response.text, features="html.parser")
article_text = soup.article.get_text(' ', strip=True)
#print("\n{}\n".format(article_text))
df.append([row["real"], article_text])
except Exception as e:
print("Error: ", end='')
print(e)
bar.next()
df = pd.DataFrame(df)
df = df.rename(columns={0: 'label', 1: 'text'})
df.to_csv('data/FNNScraped.csv')