-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrape_rss.py
45 lines (36 loc) · 1 KB
/
scrape_rss.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
#!/usr/bin/env python3
from pyquery import PyQuery as pq
from lxml import etree
import urllib
import re
import json
import sys
urls_tmp = []
urls = {
"atom": [],
"rss": []
}
def get_content_type(url):
try:
f = urllib.request.urlopen(url)
content_header = (f.info().get("Content-Type"))
if ";" in content_header:
content_splitted = content_header.split(";")
content_type = content_splitted[0]
else:
content_type = content_header
except:
content_type = "NETWORK ERROR"
return content_type
d = pq(sys.stdin.read(), parser='html')
for a in d('a,link'):
tmp = a.attrib['href']
if (re.search('^https?://.*', tmp)):
urls_tmp.append(tmp)
for link in urls_tmp:
ct = get_content_type(link)
if ct == "application/rss+xml":
urls["rss"].append(link)
elif ct == "application/atom+xml":
urls["atom"].append(link)
print (json.dumps(urls, indent=4))