-
Notifications
You must be signed in to change notification settings - Fork 0
/
similar.py
56 lines (49 loc) · 1.47 KB
/
similar.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
50
51
52
53
54
55
import urllib2
import MySQLdb
from bs4 import BeautifulSoup
def getreverse(query):
req = urllib2.Request("http://www.ask.com/web?q="+query,headers={'User-Agent':"Google Chrome"})
con=urllib2.urlopen(req)
content = con.read()
soup = BeautifulSoup(content)
related=soup.find_all("a",{"class":"related-search-item-link"})
relateds=[]
for r in related:
relateds.append(r.getText())
print r.getText()
print len(relateds)
# Open database connection
db = MySQLdb.connect(db='svvtest', user='root', passwd='', unix_socket="/opt/lampp/var/mysql/mysql.sock")
# prepare a cursor object using cursor() method
cursor = db.cursor()
# Drop table if it already exist using execute() method.
cursor.execute("DROP TABLE IF EXISTS reverselink")
# Create table as per requirement
sql = """CREATE TABLE reverselink
(
ID int NOT NULL AUTO_INCREMENT,
related text NOT NULL,
PRIMARY KEY (ID)
)"""
cursor.execute(sql)
for r in relateds:
# Prepare SQL query to INSERT a record into the database.
#sql = "INSERT INTO ask \
# (TITLE, LINK, DESC) \
# VALUES ('%s', '%s', '%s')" % (t,l,p)
query = "INSERT INTO reverselink(related) " \
"VALUES(%s)"
args = (r.encode('utf8'))
try:
# Execute the SQL command
cursor.execute(query, args)
# Commit your changes in the database
print "inserted a row!"
db.commit()
except:
# Rollback in case there is any error
print "failed"
db.rollback()
# disconnect from server
db.close()
return relateds