-
Notifications
You must be signed in to change notification settings - Fork 1
/
spyder.py
executable file
·49 lines (40 loc) · 1.3 KB
/
spyder.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
from bs4 import BeautifulSoup
import urllib.request as urllib2
#sys.stdout = os.devnull
#sys.stderr = os.devnull
def scrap(k=2):
url = "https://www.studentnewsdaily.com/archive/daily-news-article/"
req = urllib2.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
page1 = urllib2.urlopen(req)
soup = BeautifulSoup(page1)
links = [a.attrs['href'] for a in soup.find_all("a") if len(a.attrs['href'])>65]
heads = [a.string for a in soup.find_all("a") if a.attrs['href'].find("daily-news-article")]
j=0
for url in links:
j=j+1
if j > k+1:
break;
desc = ""
print("\n\n"+str(j)+"-------------------------------------------")
req = urllib2.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
page = urllib2.urlopen(req)
soup = BeautifulSoup(page,'html.parser')
s1 = soup.select("div p")
s2 = list(soup.find_all(class_="col-md-9 column"))
for i in s1:
s = i.get_text()
if s.startswith("Watch") or s.startswith("About") or s.startswith("Launched in"):
continue
desc += str(i.get_text())
#print(desc)
#print("\n"+str())
if j == 1:
continue
fileName = "parse/stories/"+str(j-1)+".story"
with open(fileName, "w") as f:
f.write(desc)
headName = "parse/heads/"+str(j-1)+".head"
with open(headName, "w") as f:
f.write(str(heads[j-1]))
if __name__ == "__main__":
scrap()