質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

selenium

Selenium(セレニウム)は、ブラウザをプログラムで作動させるフレームワークです。この原理を使うことにより、ブラウザのユーザーテストなどを自動化にすることができます。

Q&A

解決済

1回答

1759閲覧

seleniumでHTMLの要素の親子関係で絞り込んでデータを一覧で取得したい

usagiwhip

総合スコア122

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

selenium

Selenium(セレニウム)は、ブラウザをプログラムで作動させるフレームワークです。この原理を使うことにより、ブラウザのユーザーテストなどを自動化にすることができます。

0グッド

0クリップ

投稿2022/03/09 14:13

編集2022/03/09 14:22

seleniumで DIVタグで絞り込んで aタグのテキストと hrefのデータを取得したい

サンプルHTMLの
div class="details-wrapper"> で絞り込んで
<a タグ部分のテキストとリンクを一覧で取得したい

調べてみました
DIVでclassで絞り込のはできたので、それをさらにby_tag_nameで絞り込めば良さそうな情報を発見しましたがうまくいきません。
file = driver.find_elements_by_class_name('details-wrapper')
file_atag = file.find_elements_by_tag_name('a')

修正追加
file = driver.find_elements_by_class_name('details')
file_atag = file[0].find_elements_by_tag_name('a')

print(file_atag[0].text)
fileの中身が複数あると思ったので[0]を試しにつけてみたら、キーワード取得できました。

となると、 file[0]の部分をカウントアップしながらfile_atag[0].textの部分をfor文で書くとどうなるのでしょうか?

夕方からずっと悩んでなかなか解決できず。お力添えいただけないでしょうか

driver.get(url) print('ページを開く') time.sleep(5) file = driver.find_elements_by_class_name('details') file_atag = file[0].find_elements_by_tag_name('a') print(file_atag[0].text) なんか1こキーワード取れた print('~~~~~~~~~~ここまで〜〜〜〜〜〜')

サンプルHTML

<div class="details-wrapper"> <div class="index" ng-style="ctrl.blockAnimations &amp;&amp; ctrl.noTransitions" style="transition: unset;"> 1 </div> <div class="details"> <div class="details-top"> <div class="title" ng-style="ctrl.blockAnimations &amp;&amp; ctrl.noTransitions" style="transition: unset;"> <!----><span ng-repeat="titlePart in titleArray" class="" style=""> <a ng-href="/trends/explore?q=/m/0gc0prm&amp;date=now+7-d&amp;geo=JP" ng-attr-title="{{::titlePart.hoverMessage}}" track="['Trending Searches ' + ctrl.currentFeedItemType, 'click', 'item title: ' + titlePart.text]" href="/trends/explore?q=/m/0gc0prm&amp;date=now+7-d&amp;geo=JP" title="「佐津川愛美」を調べる"> 佐津川愛美 </a> <!----><span ng-if="!$last">•</span><!----> </span><!----><span ng-repeat="titlePart in titleArray" class=""> <a ng-href="/trends/explore?q=/g/122bfsrm&amp;date=now+7-d&amp;geo=JP" ng-attr-title="{{::titlePart.hoverMessage}}" track="['Trending Searches ' + ctrl.currentFeedItemType, 'click', 'item title: ' + titlePart.text]" href="/trends/explore?q=/g/122bfsrm&amp;date=now+7-d&amp;geo=JP" title="「主演」を調べる"> 主演 </a> <!----><span ng-if="!$last">•</span><!----> </span><!----><span ng-repeat="titlePart in titleArray" class=""> <a ng-href="/trends/explore?q=/m/02qfhng&amp;date=now+7-d&amp;geo=JP" ng-attr-title="{{::titlePart.hoverMessage}}" track="['Trending Searches ' + ctrl.currentFeedItemType, 'click', 'item title: ' + titlePart.text]" href="/trends/explore?q=/m/02qfhng&amp;date=now+7-d&amp;geo=JP" title="「榊 英雄」を調べる"> 榊 英雄 </a> <!----> </span><!----> </div> <i class="material-icons-extended size-24 gray hidden" ng-class="ctrl.showItemHeaderElements ? 'hidden' : 'visible'" ng-click="ctrl.share($event)" track="['Trending Searches ' + ctrl.currentFeedItemType, 'click', 'share']" ng-style="ctrl.blockAnimations &amp;&amp; ctrl.noTransitions" role="button" tabindex="0" style="transition: unset;"> share </i> </div> <div class="details-bottom"> <div ng-class="['subtitles-text-wrapper', ctrl.showItemHeaderElements ? 'visible' : 'hidden']" class="subtitles-text-wrapper visible"> <div class="summary-text" dir="ltr"> <a ng-href="https://article.auone.jp/detail/1/5/9/136_9_r_20220309_1646810141806293" target="_blank" title="「性被害」映画監督による性加害を女優が告発 脚本家からも疑問の声" track="['Trending Searches ' + ctrl.currentFeedItemType, 'click', 'article link: ' + subtitles.sanitizedArticleTitle]" bind-html-compile="subtitles.articleTitle" href="https://article.auone.jp/detail/1/5/9/136_9_r_20220309_1646810141806293">「性被害」映画監督による性加害を女優が告発 脚本家からも疑問の声</a> </div> <div class="source-and-time" title="auone.jp • 3 時間前"> <span ng-class="::{'rtl-in-ltr': ctrl.isArticleSourceRtlInLtrPage}"> auone.jp </span> • <span ng-class="::{'rtl-in-ltr': ctrl.isArticleTimeAgoRtlInLtrPage}"> 3 時間前 </span> </div> </div> <div ng-class="['subtitles-overlap', {visible: !ctrl.showItemHeaderElements, hidden: ctrl.showItemHeaderElements}]" ng-style="ctrl.blockAnimations &amp;&amp; ctrl.noTransitions" class="subtitles-overlap hidden" style="transition: unset;"> <!----> <!----><div ng-if="ctrl.currentFeedItemType === ctrl.FeedItemType.REALTIME"> 2022/03/07~現在 </div><!----> </div> </div> </div> <div ng-class="ctrl.showItemHeaderElements ? 'visible': 'hidden'" class="visible"> <!----><ng-include src="'/components/feed/feed-item-directive/feed-item-sparkline-or-search-count.html'"><!----><sparkline ng-if="storyId" service-bind="ctrl.sparklineService.getSparkline(storyId)" thickness="2" width="88" height="29" draw-area="false"><!----><div class="sparkline-chart" ng-if="width &amp;&amp; height &amp;&amp; svgPath" style=""> <svg ng-attr-width="{{ width }}" ng-attr-height="{{ height }}" viewBox="0 0 200 100" preserveAspectRatio="none" width="88" height="29"> <g fill="transparent"> <!-- Line path. --> <path ng-attr-d="M{{ ::svgPath }}" ng-attr-stroke-width="{{ thickness }}" stroke="#4284f3" vector-effect="non-scaling-stroke" d="M0,100L8.695652173913043,100L17.391304347826086,100L26.08695652173913,100L34.78260869565217,100L43.47826086956522,100L52.17391304347826,100L60.86956521739131,100L69.56521739130434,100L78.26086956521739,100L86.95652173913044,100L95.65217391304348,99L104.34782608695652,100L113.04347826086956,100L121.73913043478262,100L130.43478260869566,100L139.1304347826087,99L147.82608695652172,100L156.52173913043478,99L165.2173913043478,69L173.91304347826087,64L182.6086956521739,21L191.30434782608697,0L200,1" stroke-width="2"></path> <!-- Area path. --> <!----> </g> </svg> </div><!----> <!----><ng-transclude ng-if="width &amp;&amp; height &amp;&amp; svgPath" class="" style=""> <p class="sparkline-text"> 過去 24 時間 </p> </ng-transclude><!----> </sparkline><!----> <!----> </ng-include> </div> </div>

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

サンプルの HTML ファイルを見る限り、CSS セレクタで 'div.title > * > a' とすれば取得できそうです。

python

1file = driver.find_elements_by_css_selector('div.title > * > a') 2result = [{'text': a.text, 'link': a.get_attribute('href')} for a in file] 3 4from pprint import pprint 5pprint(result, sort_dicts=False) 6 7# 8[{'text': 'Apple', 9 'link': 'https://trends.google.co.jp/trends/explore?q=Apple&date=now+7-d&geo=JP'}, 10 {'text': '韓国大統領選挙', 11 'link': 'https://trends.google.co.jp/trends/explore?q=%E9%9F%93%E5%9B%BD%E5%A4%A7%E7%B5%B1%E9%A0%98%E9%81%B8%E6%8C%99&date=now+7-d&geo=JP'}, 12 {'text': '榊英雄', 13 'link': 'https://trends.google.co.jp/trends/explore?q=%E6%A6%8A%E8%8B%B1%E9%9B%84&date=now+7-d&geo=JP'}, 14 {'text': 'チェルノブイリ', 15 'link': 'https://trends.google.co.jp/trends/explore?q=%E3%83%81%E3%82%A7%E3%83%AB%E3%83%8E%E3%83%96%E3%82%A4%E3%83%AA&date=now+7-d&geo=JP'}, 16 {'text': 'Mac Studio', 17 'link': 'https://trends.google.co.jp/trends/explore?q=Mac+Studio&date=now+7-d&geo=JP'}, 18 {'text': '藤井聡太', 19 'link': 'https://trends.google.co.jp/trends/explore?q=%E8%97%A4%E4%BA%95%E8%81%A1%E5%A4%AA&date=now+7-d&geo=JP'}, 20 {'text': 'アイフォンse', 21 'link': 'https://trends.google.co.jp/trends/explore?q=%E3%82%A2%E3%82%A4%E3%83%95%E3%82%A9%E3%83%B3se&date=now+7-d&geo=JP'}, 22 {'text': 'オールナイトニッポン', 23 'link': 'https://trends.google.co.jp/trends/explore?q=%E3%82%AA%E3%83%BC%E3%83%AB%E3%83%8A%E3%82%A4%E3%83%88%E3%83%8B%E3%83%83%E3%83%9D%E3%83%B3&date=now+7-d&geo=JP'}, 24 {'text': '宮崎美子', 25 'link': 'https://trends.google.co.jp/trends/explore?q=%E5%AE%AE%E5%B4%8E%E7%BE%8E%E5%AD%90&date=now+7-d&geo=JP'}, 26 {'text': 'CL', 27 'link': 'https://trends.google.co.jp/trends/explore?q=CL&date=now+7-d&geo=JP'}, 28 {'text': 'ラストアイドル', 29 'link': 'https://trends.google.co.jp/trends/explore?q=%E3%83%A9%E3%82%B9%E3%83%88%E3%82%A2%E3%82%A4%E3%83%89%E3%83%AB&date=now+7-d&geo=JP'}, 30 {'text': 'ミグ29', 31 'link': 'https://trends.google.co.jp/trends/explore?q=%E3%83%9F%E3%82%B029&date=now+7-d&geo=JP'}, 32 {'text': 'ジャガー横田', 33 'link': 'https://trends.google.co.jp/trends/explore?q=%E3%82%B8%E3%83%A3%E3%82%AC%E3%83%BC%E6%A8%AA%E7%94%B0&date=now+7-d&geo=JP'}, 34 {'text': '岩田剛典', 35 'link': 'https://trends.google.co.jp/trends/explore?q=%E5%B2%A9%E7%94%B0%E5%89%9B%E5%85%B8&date=now+7-d&geo=JP'}, 36 {'text': 'iPad Air 5', 37 'link': 'https://trends.google.co.jp/trends/explore?q=iPad+Air+5&date=now+7-d&geo=JP'}, 38 {'text': 'CX-60', 39 'link': 'https://trends.google.co.jp/trends/explore?q=CX-60&date=now+7-d&geo=JP'}, 40 {'text': 'アクセンチュア書類送検', 41 'link': 'https://trends.google.co.jp/trends/explore?q=%E3%82%A2%E3%82%AF%E3%82%BB%E3%83%B3%E3%83%81%E3%83%A5%E3%82%A2%E6%9B%B8%E9%A1%9E%E9%80%81%E6%A4%9C&date=now+7-d&geo=JP'}, 42 {'text': '久慈暁子', 43 'link': 'https://trends.google.co.jp/trends/explore?q=%E4%B9%85%E6%85%88%E6%9A%81%E5%AD%90&date=now+7-d&geo=JP'}, 44 {'text': 'マクロン大統領', 45 'link': 'https://trends.google.co.jp/trends/explore?q=%E3%83%9E%E3%82%AF%E3%83%AD%E3%83%B3%E5%A4%A7%E7%B5%B1%E9%A0%98&date=now+7-d&geo=JP'}, 46 {'text': '望月理恵', 47 'link': 'https://trends.google.co.jp/trends/explore?q=%E6%9C%9B%E6%9C%88%E7%90%86%E6%81%B5&date=now+7-d&geo=JP'}]

投稿2022/03/09 17:48

melian

総合スコア19767

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

usagiwhip

2022/03/10 06:59

すみません コードの詳細について質問させてください div.title > * > a' この部分は div.titleの子要素を *でワイルド・カードでspanを絞り込み、さらにその下にあるaを絞り込むという認識であってますでしょうか?
usagiwhip

2022/03/10 07:00

あと、わかりやすい説明ありがとうございました。色々知識が増えていくと、頭の中がぐちゃぐちゃになって、勉強したことを思い出せずに困っていました。 それをスッキリリセットできました。本当ありがとうございます。
melian

2022/03/10 07:01

はい、その通りです。明示的に div.title > span > a と書いても構いません。
usagiwhip

2022/03/10 10:39

melianさん本当に助かりました。ワイルド・カードの使い方や絞り込みの方法非常に助かりました。 合わせて具体的なサンプルも助かりました。本当にありがとうございます
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問