forked from bpshaver/url-analyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
32 lines (28 loc) · 790 Bytes
/
main.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
import lxml.html
import requests
from newspaper import Article
def get_text_from_url(url):
if not url.startswith("http://"):
url = "http://" + url
a = Article(url)
a.download()
a.parse()
return a.text
def get_urls_from_url(url):
urls = []
if not url.startswith("http://"):
url = "http://" + url
res = requests.get(url)
if res.status_code == 200:
document = lxml.html.fromstring(res.text)
for a_tag in document.xpath("//a"):
try:
urls.append(
{
"text": a_tag.xpath("./text()")[0],
"url": a_tag.xpath("./@href")[0],
}
)
except IndexError:
pass
return urls