-
Notifications
You must be signed in to change notification settings - Fork 2
/
blogrollSQLGenerator.py
49 lines (40 loc) · 1.22 KB
/
blogrollSQLGenerator.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
46
47
48
49
#!/usr/bin/env python
#
# blogrollSQLGenerator.py
# Author: Ershad K <[email protected]>
# License: GPL Version 3
from BeautifulSoup import BeautifulSoup
import urllib2
url = raw_input('Enter blog url: ')
database = raw_input('Enter database name: ')
def convertToSQL(line):
try:
soup = BeautifulSoup(line)
name = soup.contents[0].a.string
link = soup.find("a")["href"]
try:
target = soup.find("a")["target"]
except:
target = ''
try:
rel = soup.find("a")["rel"]
except:
rel = ''
try:
title = soup.find("a")["title"]
except:
title = ''
sqlLine = 'INSERT into %s.wp_links (link_url, link_name, link_target, link_description, link_rel)' % database
sqlLine += """ values ('%s','%s','%s','%s','%s');""" % (link, name, target, title, rel)
print sqlLine
except:
pass
page = urllib2.urlopen(url)
pageContent = page.readlines()
index = 0
for line in pageContent:
index += 1
if line.find("<ul class='xoxo blogroll'>") > 0:
while pageContent[index].find('</ul>') == -1:
convertToSQL(pageContent[index])
index += 1