【发布时间】:2015-01-24 16:24:28
【问题描述】:
我需要存储最初存储在本地的已爬取 HTML 页面。我正在使用成功打开文件
myHTML = open(file_location,'r').read()
输出可从这里获得:http://shrib.com/jqR5uf0C
我也成功创建了 SQL 查询:
query_insert = ("insert into job_html (processid, myURL, myhtml) VALUES (%d, %s, %s)", processid, my_url, str(myHTML))
但由于某种原因,HTML 没有正确转义,因为我无法通过 Python 或手动插入它。
我也收到以下 Python 错误:
AttributeError: 'tuple' object has no attribute 'encode'
感谢 Python 新手的任何帮助 ;-)
代码:
def extractText(file_location,processid,my_url):
import mysql.connector
iwr_db = mysql.connector.connect(user='iwr', password='iwr',
host='127.0.0.1',
database='iwr_1')
myHTML = open(file_location,'r').read()
query_insert = ("insert into job_html (processid, myURL, myhtml) VALUES (%d, %s, %s)", processid, my_url, str(myHTML))
print query_insert
cursor_insert = iwr_db.cursor(buffered=True)
cursor_insert.execute(query_insert)
iwr_db.commit()
【问题讨论】:
-
a) 实际 MySQLdb 执行调用和 b) 完整回溯在哪里?
-
在同一个函数中。它就像我用正确导入的简单字符串替换 HTML 一样。当我打印 SQL 查询并尝试手动将其插入 mysql 时,它也会失败。不知道你所说的“回溯”是什么意思
-
叹息。当我说“它在哪里”时,我的意思是“请把它贴出来”。
-
谢谢 Daniel - 查看修改后的帖子