-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathScraper.py
More file actions
57 lines (43 loc) · 1.52 KB
/
Scraper.py
File metadata and controls
57 lines (43 loc) · 1.52 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
try:
from bs4 import BeautifulSoup
import requests,html2text,validators
except ImportError:
print("Please install BeautifulSoup,Requests,Html2Text,Validators Module.")
print(input())
class ScrapperClass:
title=""
paragraph=""
@staticmethod
def getUrl():
print("Enter the wikipedia URL to scrape")
url=input()
return url
@staticmethod
def getRequestToTheUrl(url):
req=requests.get(url)
return req
def scrapedContent(self,req):
sourceCode=BeautifulSoup(req.text,"html.parser")
self.title=html2text.html2text(str(sourceCode.find("h1",{"id":"firstHeading"})))[2:]
self.title=self.title.replace("\n\n","")
paragraphRaw=str(sourceCode.find("div",{"id":"mw-content-text"}))
try:
startIndex=paragraphRaw.index("</table>\n<p>")
except ValueError:
startIndex=paragraphRaw.index("</div>\n<p>")
endIndex=paragraphRaw.index('<div class="toc" id="toc">',startIndex)
self.paragraph=html2text.html2text(paragraphRaw[startIndex:endIndex])
def getTitle(self):
return self.title
def getParagraph(self):
return self.paragraph
@staticmethod
def validateUrl(url):
try:
return bool(validators.url(url).public)
except Exception as e:
isWiki = url.find("wikipedia.org")
if(isWiki != -1):
return True
else:
return False