-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrap_page.py
More file actions
61 lines (47 loc) · 1.46 KB
/
Copy pathscrap_page.py
File metadata and controls
61 lines (47 loc) · 1.46 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
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 28 12:16:59 2021
@author: sheng
"""
import sys
from bs4 import BeautifulSoup
from urllib.request import Request, urlopen
import re
def getOnline(url):
# only if requesting url from online
#req = Request("https://en.wikipedia.org/wiki/Python_(programming_language)")
req = Request(url)
html_page = urlopen(req)
return html_page
def parse(html_page):
soup = BeautifulSoup(html_page, "html.parser")
html_text = soup.get_text()
f = open("html_text.txt", "w") # Creating html_text.txt File
for line in html_text:
f.write(line)
f.close()
return html_text
def parselocal(html_page):
file = open(html_page,'r',encoding='utf8')
soup = BeautifulSoup(file,'html.parser')
html_text = soup.get_text()
return soup, html_text
if __name__ == '__main__':
default = 'default.html'
if len(sys.argv) > 1:
url = sys.argv[1]
try:
print('Trying to get url online')
html_page = getOnline(url)
print("Loaded in: ", html_page)
parse(html_page)
except:
print('Failed! Trying to get local file.')
html_page = default
else:
print('No argument inputted. Assuming default html_page')
html_page = default
# local file
print("Loaded in: ", html_page)
soup, html_text = parselocal(html_page)
info = soup.find_all("script")