/
rss1_test.py
192 lines (166 loc) · 5.8 KB
/
rss1_test.py
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import urllib2
from bs4 import BeautifulSoup
import lxml.html as html1
from goose import Goose
import re
import json
import feedfinder
import requests
from lxml.html.clean import Cleaner
# import sys
# sys.setdefaultencoding('utf-8')
linkPattern = re.compile("^(?:ftp|http|https):\/\/(?:[\w\.\-\+]+:{0,1}[\w\.\-\+]*@)?(?:[a-z0-9\-\.]+)(?::[0-9]+)?(?:\/|\/(?:[\w#!:\.\?\+=&%@!\-\/\(\)]+)|\?(?:[\w#!:\.\?\+=&%@!\-\/\(\)]+))?$")
job=re.compile('.*rss.*|.*feeds.*|.*rssfeeds.|.*source=feedburner.*|.*feed.*',re.IGNORECASE)
r1=re.compile('.*live.*|.*google.*|.*yahoo.*|.*subscription.*|.*login.*|.*signup.*|.*twitter.*|.*facebook.*|.*pdf.*',re.IGNORECASE)
urls=[]# add url
l=[]
#print url
#html = requests.get(url)
#soup = BeautifulSoup(html.content)
#print "d"
blogs = []
crawledLink = []
d={}
v = {}
fin={}
ill=50
count = 0
def crawl(link1):
try:
global depth
depth=depth+1
if depth < 10:
#count=count+1
html=requests.get(link1)
soup1=BeautifulSoup(html.content)
#print soup1
# print '##########'
# print soup1.find_all('link')
# print soup1.find_all('link/')
# listOfLinks = []
# listOfLinks.extend(soup1.find_all('a'))
#listOfLinks.extend()
for l in soup1.find_all('a'):
l1=str(l.get('href')).encode("utf-8")
if not l1 in crawledLink and job.match(l1) and not r1.match(l1):
#print '222222'
crawledLink.append(l1)
if not linkPattern.match(l1):
if l1[0]!='/':
l1=url+'/'+l1
else:
l1=url+l1
#print l1
if not l1 in blogs:
blogs.append(l1)
a=crawl(l1)
except:
pass
#else:
#count=0
for url in urls:
try:
html = requests.get(url)
soup = BeautifulSoup(html.content)
name=soup.title.string
fr=feedfinder.feeds(url)
print (fr)
ill=ill+1
# filname=url.replace('http://' or 'https://','')
# filname=filname.replace('.*www.','')
# filname=filname.replace('.com','')
# filname=filname.replace('.org.*','')
# print filname
blogs=[]
with open('link%i.json'%ill,"w") as outfile:
html = requests.get(url)
soup = BeautifulSoup(html.content)
title=soup.title.string
for link in soup.find_all('a'):
#print "111111111111111111"
link1 = str(link.get('href')).encode('ascii','ignore')
flag=0
if job.match(link1) and not r1.match(link1):
# flag=1
# if flag=='0':
# blogs.append(fr)
# else:
#print '333333'
if not link1 in crawledLink:
#print '4444444'
crawledLink.append(link1)
if not linkPattern.match(link1):
#print '5555555555'
if link1[0]!='/':
link1=url+'/'+link1
else:
link1=url+link1
depth = 0
#blogs.append(link1)
#print '666666666'
print link1
if len(fr)>0:
#print"QqQQQQqqqQQQQ"
blogs.append(link1)
else:
a=crawl(link1)
else:
blogs=fr
except:
pass
print "project -------->>>>> ",blogs
l=[]
li=[]
for a in blogs:
try:
html = requests.get(a)
soup = BeautifulSoup(html.content)
html45 = html1.parse(a).xpath('//pubdate//text()')
print html45
if soup.find_all('guid'):
for link in soup.find_all('guid'):
#print '@@@@@@@@@@@@@@@@@@@@@@'
az = str(link.get_text()).encode('ascii','ignore')
#az = str(link.get('href')).encode('ascii','ignore')
#print az
if az not in l:
l.append(az)
else:
for link in soup.find_all('comments'):
az = str(link.get_text()).encode('ascii','ignore')
if az not in l:
l.append(az)
print l
except:
pass
i=0
for al in l:
try:
# print "11111111111"
#print al
#html = html1.parse(al).xpath("//h5[@class='itemposttime']")
#print "$$$$$$$$$$$"
#print html
d={}
g = Goose({'browser_user_agent': 'Mozilla'})
g=Goose()
art=g.extract(al)
#print art.title
title = art.title
text=art.cleaned_text.encode('ascii','ignore')
#print "vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv"
#print text
d["title"] = title
d["description"] = text
d["rss link"] = al
#print "222222222222222222"
d["updatedDate"]=html45[i]
i=i+1
#print html45[1]
li.append(d.copy())
except:
pass
#print li
v["company_name"] = name
v["blogs"]=li
print v