Pyhonで下記のコードを書いたところ、BeautifulSoupで.textを使うとエラーが出ます。
.textを使わなければエラーは出ないのですが、タグ内の文字だけを抽出したいため、困っております。よろしければ、.textを使ってもエラーが出ない方法、もしは他の方法でテキストを抽出する方法をご存知でしたらご教授お願いいたします。
from time import sleep
from bs4 import BeautifulSoup
import requests
url = 'https://alert.shop-bell.com/books/magazine/3/'
r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
comics = soup.find_all('tr')
d_list = []
for comic in comics:
title = comic.find('a').text
author = comic.find('span', attrs={'class': 'muted'}).text
release_date = comic.find('td', attrs={'class': 'srpLatest'}).text.split('巻')[1]
d = {'title': title, 'author': author, 'release_date': release_date}
d_list.append(d)
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
回答3件
0
自己解決
下記の部分を修正したらいけました。ご回答いただいた皆様ありがとうございました。↓
release_date = comic.find('td', attrs={'class': 'srpLatest'}).text.split('巻')[1]
修正後↓
release_date = comic.find('td', attrs={'class': 'srpLatest'}).text
if '巻' in release_date:
release_date = release_date.split('巻')[1]
投稿2022/01/16 10:20
総合スコア36
0
python
1comics = soup.find('tbody').find_all('td') 2 3for comic in comics: 4 title = comic.find('a').text 5 author = comic.find('span', attrs={'class': 'muted'}).text 6 if '巻' in comic.find('td', attrs={'class': 'srpLatest'}).text: 7 release_date = comic.find('td', attrs={'class': 'srpLatest'}).text.split('巻')[1] 8 d = {'title': title, 'author': author, 'release_date': release_date} 9 d_list.append(d) 10 11from pprint import pprint 12pprint(d_list)
ぐらいでしょうか。
実行結果
python
1>>> pprint(d_list) 2[{'author': '稲岡和佐', 'release_date': '2021年09月03日 発売', 'title': 'アイテルシー'}, 3 {'author': '三浦糀', 'release_date': '2022年01月04日 発売', 'title': 'アオのハコ'}, 4 {'author': '中村充志', 'release_date': '2021年07月02日 発売', 'title': 'AGRAVITY BOYS'}, 5 {'author': 'みたらし三大,たけぐし一本', 6 'release_date': '2021年12月03日 発売', 7 'title': 'アメノフル'}, 8 {'author': '矢吹健太朗', 'release_date': '2022年01月04日 発売', 'title': 'あやかしトライアングル'}, 9 {'author': '稲岡和佐', 'release_date': '2021年09月03日 発売', 'title': 'アイテルシー'}, 10 {'author': '三浦糀', 'release_date': '2022年01月04日 発売', 'title': 'アオのハコ'}, 11 {'author': '中村充志', 'release_date': '2021年07月02日 発売', 'title': 'AGRAVITY BOYS'}, 12 {'author': 'みたらし三大,たけぐし一本', 13 'release_date': '2021年12月03日 発売', 14 'title': 'アメノフル'}, 15 {'author': '矢吹健太朗', 'release_date': '2022年01月04日 発売', 'title': 'あやかしトライアングル'}, 16 {'author': '賀来ゆうじ', 'release_date': '2022年01月04日 発売', 'title': 'アヤシモン'}, 17 {'author': '戸塚慶文', 'release_date': '2021年12月03日 発売', 'title': 'アンデッドアンラック'}, 18 {'author': '篠原健太', 'release_date': '2021年11月04日 発売', 'title': 'ウィッチウォッチ'}, 19 {'author': '小山ゆうじろう,イーピャオ', 20 'release_date': '2021年11月04日 発売', 21 'title': '巻末解放区!WEEKLY 週ちゃん'}, 22 {'author': '福井あしび,鎌田悠幹', 23 'release_date': '2021年10月04日 発売', 24 'title': 'クーロンズ・ボール・パレード'}, 25 {'author': '仲間りょう', 'release_date': '2021年11月04日 発売', 'title': '高校生家族'}, 26 {'author': '秋本治', 'release_date': '2021年10月04日 発売', 'title': 'こちら葛飾区亀有公園前派出所'}, 27 {'author': '鈴木祐斗', 'release_date': '2022年01月04日 発売', 'title': 'SAKAMOTO DAYS'}, 28 {'author': '田村隆平', 'release_date': '2021年10月04日 発売', 'title': '灼熱のニライカナイ'}, 29 {'author': '長谷川智広', 'release_date': '2021年04月02日 発売', 'title': '森林王者モリキング'}, 30 {'author': '芥見下々', 'release_date': '2021年12月25日 発売', 'title': '呪術廻戦'}, 31 {'author': '藤本タツキ', 'release_date': '2021年03月04日 発売', 'title': 'チェンソーマン'}, 32 {'author': '稲垣理一郎,Boichi', 33 'release_date': '2022年01月04日 発売', 34 'title': 'Dr.STONE'}, 35 {'author': '大須賀玄', 'release_date': '2022年01月04日 発売', 'title': 'ドロンドロロン'}, 36 {'author': '松井優征', 'release_date': '2022年01月04日 発売', 'title': '逃げ上手の若君'}, 37 {'author': '比良賀みん也', 'release_date': '2022年01月04日 発売', 'title': 'NERU 武芸道行'}, 38 {'author': '上木敬', 'release_date': '2022年01月04日 発売', 'title': '破壊神マグちゃん'}, 39 {'author': '冨樫義博', 'release_date': '2018年10月04日 発売', 'title': 'HUNTER×HUNTER'}, 40 {'author': '島袋光年', 'release_date': '2021年09月03日 発売', 'title': 'BUILD KING'}, 41 {'author': 'マポロ3号', 'release_date': '2022年01月04日 発売', 'title': 'PPPPPP'}, 42 {'author': '田畠裕基', 'release_date': '2022年01月04日 発売', 'title': 'ブラッククローバー'}, 43 {'author': '松浦健人', 'release_date': '2021年07月02日 発売', 'title': '仄見える少年'}, 44 {'author': '宮崎周平', 'release_date': '2022年01月04日 発売', 'title': '僕とロボコ'}, 45 {'author': '堀越耕平', 'release_date': '2021年10月04日 発売', 'title': '僕のヒーローアカデミア'}, 46 {'author': '甲本一', 'release_date': '2021年12月03日 発売', 'title': 'マッシュル-MASHLE-'}, 47 {'author': '伊原大貴', 'release_date': '2021年12月03日 発売', 'title': '守れ!しゅごまる'}, 48 {'author': '権平ひつじ', 'release_date': '2022年01月04日 発売', 'title': '夜桜さんちの大作戦'}, 49 {'author': '川口勇貴', 'release_date': '2022年01月04日 発売', 'title': 'レッドフード'}, 50 {'author': '尾田栄一郎', 'release_date': '2021年12月03日 発売', 'title': 'ONE PIECE'}]
投稿2022/01/15 13:06
総合スコア24666
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
0
python
1from time import sleep 2from bs4 import BeautifulSoup 3import requests 4 5url = 'https://alert.shop-bell.com/books/magazine/3/' 6r = requests.get(url) 7soup = BeautifulSoup(r.text, 'html.parser') 8 9comics = soup.select('table#reflow > tbody > tr') 10d_list = [] 11for comic in comics: 12 title = comic.select_one('a').text 13 author = comic.select_one('span.muted').text 14 release_date = comic.select_one('td.srpLatest').text 15 if '巻' in release_date: 16 release_date = release_date.split('巻')[1] 17 d = {'title': title, 'author': author, 'release_date': release_date} 18 d_list.append(d) 19 20from pprint import pprint 21pprint(d_list, width=50, sort_dicts=False)
投稿2022/01/15 12:53
編集2022/01/15 13:32総合スコア19791
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/01/15 13:03
2022/01/15 13:14
2022/01/15 14:04
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。