Chr*_*ris 2 python sqlite web-scraping
我正在尝试使用Python和BeautifulSoup来抓取一些Web信息,迭代它然后将一些部分插入到sqlite3数据库中.但我不断提出这个错误:
文件"/Users/Chris/Desktop/BS4/TBTfile.py",第103行,TBTscrape c.execute(item)sqlite3.OperationalError:near"s":语法错误
相同的代码在非常相似的刮刀上工作正常,并且不会抛出这些错误.这是它的一部分:
listicle.append("INSERT INTO headlines (heds, url, image_loc, time, source) VALUES ('" + TBTheadline + "', '" + TBTurl + "', '" + imageName + "', '" + TBTpostDate + "', '" + source + "')")
else:
print "TBT item already in database"
print listicle
for item in listicle:
c.execute(item)
conn.commit()
row = c.fetchall()
print "This has been inserted succcessfully: ", item
Run Code Online (Sandbox Code Playgroud)
您将收集的数据连接到SQL语句中.永远不要那样做,它是所有反模式的母亲.除了您遇到的问题(可能是由于HTML中的'或类似字符),您的代码中存在一个巨大的安全漏洞(在您的情况下可能或可能不重要).
无论如何,sqlite3有一个很好的方式来做你想要的:executemany.在你的情况下
listicle.append((TBTheadline,TBTurl,imageName,TBTpostDate,source))
conn.executemany("INSERT INTO headlines (heds, url, image_loc, time, source) VALUES (?,?,?,?,?)", listicle)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3486 次 |
| 最近记录: |