Python_MySQL:AttributeError:';功能';对象没有属性';翻译';
运行以下代码python代码时,我得到以下代码:Python_MySQL:AttributeError:';功能';对象没有属性';翻译';,python,mysql,Python,Mysql,运行以下代码python代码时,我得到以下代码: from urllib.request import urlopen from bs4 import BeautifulSoup import datetime import random import pymysql import re conn = pymysql.connect(host='127.0.0.1', user='root', passwd ='mypass', db='mysql',charset='utf8') cur
from urllib.request import urlopen
from bs4 import BeautifulSoup
import datetime
import random
import pymysql
import re
conn = pymysql.connect(host='127.0.0.1', user='root', passwd ='mypass',
db='mysql',charset='utf8')
cur = conn.cursor()
cur.execute('USE scraping')
random.seed(datetime.datetime.now())
def store(title, content):
cur.execute("DROP TABLE IF EXISTS pages")
sql = """CREATE TABLE pages (id BIGINT(7) NOT NULL AUTO_INCREMENT, title VARCHAR(200)
, content VARCHAR(10000), created TIMESTAMP DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY(id))"""
cur.execute(sql)
cur.execute("""INSERT INTO pages (title, content) VALUES ("%s", "%s")""", (title, content))
cur.connection.commit()
def getLinks(articleUrl):
html = urlopen('http://en.wikipedia.org'+articleUrl)
bs = BeautifulSoup(html, 'html.parser')
title = bs.find('h1').get_text()
content = bs.find('div', {'id':'mw-content-text'}).find('p').get_text
store(title, content)
#return bs.find('div',{'id':'bodyContent'}).findAll('a',href=re.compile('^(/wiki/)((?!:).)*$'))
links = getLinks('/wiki/Kevin_Bacon')
#print(links)
我收到以下错误消息:
AttributeError: 'function' object has no attribute 'translate'
据我所知,代码中的失败点似乎在这一点上:
cur.execute("""INSERT INTO pages (title, content) VALUES ("%s", "%s")""", (title, content))
我已尝试通过查看以下内容来解决问题:
- File "C:\Users\mypath\PycharmProjects\Scraper\venv\lib\site-packages\pymysql\converters.py", line 118, in escape_unicode
return u"'%s'" % _escape_unicode(value)
- File "C:\Users\mypath\PycharmProjects\Scraper\venv\lib\site-packages\pymysql\converters.py", line 73, in _escape_unicode
return value.translate(_escape_table)
关于问题的起因,您有什么想法吗?您忘记在
get\u text
函数调用中添加括号,应该是:
content = bs.find('div', {'id':'mw-content-text'}).find('p').get_text()
工作!谢谢你的帮助!