我在一个网站上找到了这个示例代码,并尝试根据我希望它的工作方式来实现它。不幸的是,原来的代码工作得很好,有多行被刮下的项目,但我修改的那一行只插入了1个项目。
from urllib import urlopen
from bs4 import BeautifulSoup
import pymysql.cursors
html = urlopen("https://www.banggood.com/Wholesale-Computer-and-Networking-c-155.html")
bsObj = BeautifulSoup(html, "html.parser")
recordList = bsObj.findAll("div", {"class": "wrap overflow", })
connection = pymysql.connect(host='localhost',
user='root',
password='',
db='db',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
try:
with connection.cursor() as cursor:
for record in recordList:
title = record.find("span", {"class": 'title', }).get_text().strip()
artist = record.find("span", {"class": "price wh_cn"}).get_text().strip()
sql = "INSERT INTO `artist_song` (`artist`, `song`) VALUES (%s, %s)"
cursor.execute(sql, (artist, title))
connection.commit()
finally:
connection.close()
我做错了什么?
暂无答案!
目前还没有任何答案,快来回答吧!