运行以下代码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 = 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)
对可能导致此问题的原因有什么想法?
答案 0 :(得分:0)
您忘记在content = bs.find('div', {'id':'mw-content-text'}).find('p').get_text()
函数调用中添加括号,应该是:
$(function(){
$("#includedContent").load("Menu.html");
// $("#datepicker1").datepicker({
// showOn: "both",
// format: 'dd/mm/yyyy',
// onSelect: function(dateText, inst){
// $("#datepicker2").datepicker("option","minDate",
// $("#datepicker1").datepicker("getDate"));
// }
// });
//
// $("#datepicker2").datepicker({
// showOn: "both",
// format: 'dd/mm/yyyy',
// onSelect: function(dateText, inst){
// $("#datepicker1").datepicker("option","maxDate",
// $("#datepicker2").datepicker("getDate"));
// }
// });
// $("#datepicker1").datepicker({
// showOn: "both",
// format: 'dd/mm/yyyy', //date formated correctly
// maxDate: $("#datepicker2").datepicker("getDate")
// });
// $("#datepicker2").datepicker({
// showOn: "both",
// format: 'dd/mm/yyyy', //Does not format the address it is returned as mm/dd/yyyy
// minDate: $("#datepicker1").datepicker("getDate")
// });
$("#datepicker1").datepicker({
dateFormat: 'dd/mm/yyyy', //Does not format the address it is returned as mm/dd/yyyy
inline: true,
maxDate: $("#datepicker2").datepicker("getDate") });
$("#datepicker2").datepicker({
dateFormat: 'dd/mm/yyyy', //Does not format the address it is returned as mm/dd/yyyy
inline: true,
minDate: $("#datepicker1").datepicker("getDate") });
// $('#datepicker1').datepicker({
// format: 'dd/mm/yyyy',
// });
// $('#datepicker2').datepicker({
// useCurrent: false, //Important! See issue #1075
// format: 'dd/mm/yyyy',
// });
// $("#datepicker1").on("dp.change", function (e) {
// $('#datepicker2').data("DatePicker").minDate(e.date);
// });
// $("#datepicker2").on("dp.change", function (e) {
// $('#datepicker1').data("DatePicker").maxDate(e.date);
// });
});
$(document).ready(function(){
$('[data-toggle="tooltip"]').tooltip();
}); // end document.ready