LoginSignup
hiha1323
@hiha1323

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

スクレイピングで同じクラスが複数あるものを一つずつ指定するにはどのようにすればいいですか

解決したいこと

こちらのサイトhttps://github.com/orangain/scraping-hands-on/blob/master/exercises.md でpythonのスクレイピングの練習しております。1つ目の問題で同じクラスが複数あり一番最初の一つだけしか取得できません。どのようにすれば一つずつ取得できますか?7

該当するソースコード

import requests
from bs4 import BeautifulSoup

url = "http://qiita.com/advent-calendar/2016/crawler"
html = requests.get(url)
soup = BeautifulSoup(html.content,'html.parser')

#木曜日1日
for link in soup.find(class_='style-1dctyxx'):
    print(link.get('href'))
for element in soup.find(class_='style-3ki7ar'):
    print(element.text)
#金曜日2日
for link in soup.find(class_='style-1dctyxx'):
    print(link.get('href'))
for element in soup.find(class_='style-3ki7ar'):
    print(element.text)

出力)

http://amacbee.hatenablog.com/entry/2016/12/01/210436
scrapy-splashを使ってJavaScript利用ページを簡単スクレイピング
http://amacbee.hatenablog.com/entry/2016/12/01/210436
scrapy-splashを使ってJavaScript利用ページを簡単スクレイピング

pythonのバージョンは3.11.2です。

0

1Answer

soup.findとしている箇所をsoup.find_allとすれば、該当クラスのものを全て取ってこれると思います。
コード内linkelementは、元々配列の要素を想定していると思うので、上記変更だけで動いてくれると思います。
(手元環境での動作確認をしていないので、もしかすると動いてくれないかもしれません。すみません……)

0

Your answer might help someone💌