さくらレンタルサーバを(スタンダード)使用しています。
(FreeBSD 9.1-RELEASE-p24)
(Python 3.8.9)
pipインストールリスト
beautifulsoup4 4.10.0
certifi 2021.10.8
chardet 4.0.0
charset-normalizer 2.0.12
cssselect 1.1.0
idna 3.3
lxml 4.8.0
mysqlclient 2.1.0
pip 22.0.4
requests 2.27.1
setuptools 49.2.1
soupsieve 2.3.1
urllib3 1.26.9
wheel 0.37.1
以下のコードで、print表示した場合変数の値(ランキングやクラブネーム)は表示されているのですが、
MySQLにINSERTした場合、値が0として入力されます。
お詳しい型ご教授いただけますと幸いです。
MySQLdb モジュールでデータベースに接続する
conn = MySQLdb.connect(host='データベースサーバ', db='データベース名',user='ユーザー名',passwd='パスワード',charset='utf8mb4')
カーソル作成
cur = conn.cursor()
url = "https://www.jleague.jp/standings/j1/"
f = urllib.request.urlopen(url)
soup = BeautifulSoup(f, "html.parser")
テーブル(表)要素の全取得
table = soup.find_all('table', class_='scoreTable01')
全クラブネームの取得
all_Club_Name = soup.find_all('td', class_='tdTeam')
Ranking = 1
for tmp in all_Club_Name:
# 現在の日付 d = datetime.date.today() # ランキング Ranking = Ranking + 1 # クラブネーム1要素ずつ出力#get_text()で文字データを取得する。 s = tmp.get_text() # 文字数の半分の数を計算 half = len(tmp.get_text()) // 2 # 前半半分を抽出 Club_Name = s[:half] # 次の要素(勝点) Win_Points = tmp.next_sibling.next_sibling query = 'insert into j1jyunni value (Ranking,Club_Name,Win_Points,0,0,0,0,0,0,0)' cur.execute(query)
print(Ranking)
conn.commit()
回答1件
あなたの回答
tips
プレビュー