-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent-parser.py
More file actions
150 lines (120 loc) · 4.65 KB
/
Copy pathcontent-parser.py
File metadata and controls
150 lines (120 loc) · 4.65 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import json
import pandas as pd
from newspaper import Article
import newspaper
import requests
import urllib3
import urllib
import lxml
from readable_content.parser import ContentParser
import csv
urls = pd.read_csv("test.csv")
text_dict = {}
count = 0
img_urls = ["icedrive.com", "smugmug.com", "photos.google.com", "photoblog.com", "icloud.com","dropbox.com", "4shared.com",
"imgshack", "flickr.com", "imgur.com", "freeimages.com", "imgbox.com","cluster.com", "postimages.org",
"freeimagehosting.net", "deviantart.com", "unsplash.com", "500px.com", "pxhere.com", "pixabay.com"]
count_1 = 0
count_2 = 0
count_3 = 0
count_4 = 0
count_5 = 0
count_6 = 0
count_7 = 0
error_file = "errors-content-parser.csv"
def image_download(url, img_urls, count):
for j in range(len(img_urls)):
if img_urls[j] in url:
response = requests.get(url)
file = open(str(count) + ".png", "wb")
file.write(response.content)
file.close()
return -1
for i in urls["url"]:
while True:
try:
flag = 0
if i[0:7] != "http://":
if i[0:8] == "https://":
flag = 1
elif flag != 1:
prefix = "http://"
i = prefix + i
count = count+1
print(i, count)
if image_download(i, img_urls, count) == -1:
break
response = requests.get(i)
article = Article(i)
article.download()
article.parse()
parser = ContentParser(i)
content = parser.get_content()
text_dict["Title"] = article.title
text_dict["Title"] = text_dict["Title"].replace("\n","")
text_dict["Title"] = text_dict["Title"].replace(r"\u","")
text_dict["URL"] = article.url
text_dict["Effective URL"] = [resp.url for resp in response.history]
text_dict["Date"] = article.publish_date
text_dict["Date"] = str(text_dict["Date"])
text_dict["Authors"] = article.authors
text_dict["Text"] = content
text_dict["Text"] = text_dict["Text"].replace("\n","")
text_dict["Text"] = text_dict["Text"].replace(r"\u","")
file_name = str(count)
test_json = open(file_name+".json", "w")
json.dump(text_dict, test_json)
test_json.close()
with open(error_file,"a") as errors:
writer = csv.writer(errors)
writer.writerow([count, i, "No errors"])
break
except newspaper.article.ArticleException as e:
count_1 = count_1+1
print(e, count_1)
with open(error_file,"a") as errors:
writer = csv.writer(errors)
writer.writerow([count, i, e, count_1])
break
except requests.exceptions.TooManyRedirects as e:
count_2 = count_2+1
print(e,count_2)
with open(error_file,"a") as errors:
writer = csv.writer(errors)
writer.writerow([count, i, e, count_2])
break
except UnicodeError as e:
count_3 = count_3 + 1
print(e, count_3)
with open(error_file,"a") as errors:
writer = csv.writer(errors)
writer.writerow([count, i, e, count_3])
break
except requests.exceptions.ConnectionError as e:
count_4 = count_4 + 1
print(e,count_4)
with open(error_file,"a") as errors:
writer = csv.writer(errors)
writer.writerow([count, i, e, count_4])
break
except urllib3.exceptions.LocationParseError as e:
count_5 = count_5 + 1
print(e, count_5)
with open(error_file,"a") as errors:
writer = csv.writer(errors)
writer.writerow([count, i, e, count_5])
break
except urllib.error.URLError as e:
count_6 =count_6 + 1
print(e, count_6)
with open(error_file,"a") as errors:
writer = csv.writer(errors)
writer.writerow([count, i, e, count_6])
break
except lxml.etree.ParserError as e:
count_7 =count_7 + 1
print(e, count_7)
with open(error_file,"a") as errors:
writer = csv.writer(errors)
writer.writerow([count, i, e, count_7])
break