実現したいこと
pythonを使用してスクレイピングしたデータをMySQLに保存したい
発生している問題・エラーメッセージ
Pythonのライブラリpymysqlを使用して
データをINSERTしようとすると、エラーが出てきます。
pymysql.err.OperationalError: (1054, "Unknown column 'name' in 'field list'")
該当のソースコード
python
1import requests 2from bs4 import BeautifulSoup 3import pymysql 4 5connection = pymysql.connect(host='127.0.0.1', 6 user='root', 7 password='●●●●●●●●●●', 8 database='mysql', 9 charset='utf8') 10 11with connection: 12 with connection.cursor() as cursor: 13 cursor.execute("USE 〇〇〇〇") 14 15 with connection.cursor() as cursor: 16 project_name_list = [] 17 amount_list = [] 18 supporter_list = [] 19 20 url_list = ['https://readyfor.jp/projects/starsphere'] 21 for url in url_list: 22 response = requests.get(url) 23 soup = BeautifulSoup(response.content, 'html.parser') 24 25 try: 26 project_name = soup.find('h1', {'class':'css-yydcpv'}).text 27 project_name_list.append(project_name) 28 29 amount = soup.find('span', {'class':'css-5681bh'}).text 30 amount_list.append(amount) 31 32 supporter = soup.find('dd', {'class','css-8wsyft'}).text 33 supporter_list.append(supporter) 34 35 sql = " INSERT INTO project(name, amount, supporter, web) VALUES (%s, %s, %s, %s) " 36 cursor.execute(sql, (project_name, amount, supporter, url)) 37 38 connection.commit() 39 40 except AttributeError: 41 pass 42 43
試したこと
VALUESの後の(%s, %s, %s, %s)を('%s', '%s', '%s', '%s')
に変更して実行すると
pymysql.err.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '●●●'', ''1' at line 1")
補足情報(FW/ツールのバージョンなど)
Python3.10.0
mysql Ver 8.0.31 for macos12.6 on arm64 (Homebrew)

回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。
2022/11/15 09:11