python 에서 mysql 연동
웹에서 가져온 html을 mysqldb에 저장하려고 합니다.
>>> import urllib2
>>> from urlparse import urljoin
>>> from bs4 import BeautifulSoup
>>> import MySQLdb
>>> db = MySQLdb.connect(host="localhost", user="root", passwd="asdf0731", db="apache")
>>> cursor = db.cursor()
>>> url=urllib2.urlopen('http://www.apache.org')
>>> s=BeautifulSoup(url).find_all('a')
>>>
>>> for parsed_url in s:
... cursor.execute("INSERT INTO test(url) VALUE ({0})".format(urljoin(url, parsed_url["href"])))
...
Traceback (most recent call last):
File "", line 2, in
File "/usr/lib/python2.7/urlparse.py", line 259, in urljoin
urlparse(base, '', allow_fragments)
File "/usr/lib/python2.7/urlparse.py", line 143, in urlparse
tuple = urlsplit(url, scheme, allow_fragments)
File "/usr/lib/python2.7/urlparse.py", line 182, in urlsplit
i = url.find(':')
AttributeError: addinfourl instance has no attribute 'find'
에러가 안뜨려면 어딜 고쳐야하는건가요?ㅠㅠ
댓글 달기