我试图使下面的代码更改第22行的打印响应以将其写入数据库。
我碰到了一堵砖墙,却找不到足够类似的例子来指导我解决这个问题,因此,如果有人能指出正确的方向,我将永远感激不已: / p>
import urllib2
import MySQLdb
site= "https://www.example.com/sitemap.xml"
hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
'Accept-Encoding': 'none',
'Accept-Language': 'en-US,en;q=0.8',
'Connection': 'keep-alive'}
req = urllib2.Request(site, headers=hdr)
try:
page = urllib2.urlopen(req)
except urllib2.HTTPError, e:
print e.fp.read()
content = page.read()
print content
print ("Start DB")
db = MySQLdb.connect("127.0.0.1","root","Password!","dbname" )
cursor = db.cursor()
sql = "INSERT INTO xmlsitemaps(xmlurl) VALUES ('content')"
try:
# Execute the SQL command
cursor.execute(sql)
# Commit changes in the database
db.commit()
except:
# Rollback in case there is any error
db.rollback()
db.close()
print ("End DB")
感谢您的光临!