forked from REMitchell/python-scraping
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1-wikiUnitTest.py
More file actions
52 lines (43 loc) · 1.5 KB
/
1-wikiUnitTest.py
File metadata and controls
52 lines (43 loc) · 1.5 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
from urllib.request import urlopen
from urllib.parse import unquote
import random
import re
from bs4 import BeautifulSoup
import unittest
class TestWikipedia(unittest.TestCase):
bsObj = None
url = None
def test_PageProperties(self):
global bsObj
global url
url = "http://en.wikipedia.org/wiki/Monty_Python"
#Test the first 100 pages we encounter
for i in range(1, 100):
bsObj = BeautifulSoup(urlopen(url))
titles = self.titleMatchesURL()
self.assertEquals(titles[0], titles[1])
self.assertTrue(self.contentExists())
url = self.getNextLink()
print("Done!")
def titleMatchesURL(self):
global bsObj
global url
pageTitle = bsObj.find("h1").get_text()
urlTitle = url[(url.index("/wiki/")+6):]
urlTitle = urlTitle.replace("_", " ")
urlTitle = unquote(urlTitle)
return [pageTitle.lower(), urlTitle.lower()]
def contentExists(self):
global bsObj
content = bsObj.find("div",{"id":"mw-content-text"})
if content is not None:
return True
return False
def getNextLink(self):
global bsObj
links = bsObj.find("div", {"id":"bodyContent"}).findAll("a", href=re.compile("^(/wiki/)((?!:).)*$"))
link = links[random.randint(0, len(links)-1)].attrs['href']
print("Next link is: "+link)
return "http://en.wikipedia.org"+link
if __name__ == '__main__':
unittest.main()