Pythonを使ってBeutifulSoupでチャットIDを取得したいです。
取得条件としてはチャットメッセージに”ORANGE”の用語が含んでいましたら、
ID取得したいです。
HTML
<div id="section-post-1862374024" class="article-container _updatedData"> <!-- article hedaer START --> <div class="article-header _profileAnchor flex-row-box "> <!-- 사용자 프로필 컨테이너 (medium) --> <div class="user-profile-container size-large flex-fix _user _profileImage " data-id="17515244"> <!-- 사용자 프로필 이미지 --> <img class="img-user-profile" src="https://example-box.com/files-profile/f0b6e5c90870c2fb50290fd3152b7b2a?size=80" onerror="this.onerror=null;this.src='https://cdn.example.com/app/assets/images/b4084919.profile_default_240_20180830.png'" alt="user profile image"> <!-- 부재중 상태 표시 레이어 --> <div class="overlay-user-profile"> <i class="icon-svg ico-away"></i> </div> <!-- 차단된 상태 표시 레이어--> <div class="overlay-user-profile blocked"> <i class="icon-ic-block"></i> </div> </div> <div class="article-profile flex-rel ellip-1"> <div class="fn-user-name member-name selectable cursor_pointer fn-13 _profileName _user">TA bot<span class="member-state-meessage"></span></div> <div class="article-date fn-11"> <span class="ng-binding">2021/11/03 13:28</span> </div> </div> <div class="article-action flex-fix fn-16 "> <ul class="snb-menu-group board-snb"> <li class=""> <div class="board-menu-btn"> <i class="icon-ic-more _postMore _posBottom"></i> </div> </li> <li class=""> <div class="board-menu-btn"> <i class="icon-ic-star _star _star-1860359798 off"></i> </div> </li> </ul> </div> </div> <!-- article hedaer END --> <!-- article body START --> <div class="article-body"> <div class="article-title fn-16"> <span class="selectable">TEST4</span> </div> <!-- article-contents START --> <div class="article-contents"> <!-- 게시글 본문 --> <span class="selectable msg-text-box _compile ng-scope">ORANGE</span> <!-- 게시글 본문 (끝) --> </div> <!-- article-contents END --> <div class="_compile ng-scope"> </div>
実現したい結果
id="section-post-1862374024"もしくはpost-id="1862374024" 1862374024の数字IDを取得したいです。
下記方法で行いましたが、取得したいID何も表示されないです。
現在の結果
[]
複数条件でIDを取得する為に何を修正すれば良いでしょうか。
もし分かる方がいましたら、教えていただけると幸いです。
# coding:utf-8 from selenium import webdriver from selenium.webdriver.common.keys import Keys import time from selenium.webdriver.chrome.options import Options from selenium.webdriver.support.select import Select from selenium.webdriver.remote.webelement import WebElement #from datetime import datetime as dt, date, timedelta import pyautogui from bs4 import BeautifulSoup #headless background option = Options() option.add_argument('--headless') #Getting Default Adapter failed error message option.add_experimental_option('excludeSwitches', ['enable-logging']) #strDate = dt.now().strftime("%Y%m%d") URL= "https://www.example.com/landing/jp/signin/" # ブラウザを開く。 #options=option background driver = webdriver.Chrome(executable_path="C:\Program Files\chromedriver_win32\chromedriver.exe") # Googleの検索TOP画面を開く。 driver.get(URL) #2秒待機 time.sleep(2) #login_idを入力 login_id = driver.find_element_by_name("email") login_id.send_keys("example@.com") #passwordを入力 password = driver.find_element_by_name("unknown") password.send_keys("12131") #ログインボタンをクリック login = driver.find_element_by_xpath('//*[@id="__next"]/div/div[1]/form/button') login.click() #1秒待機 time.sleep(2) #ボタンをクリック button = driver.find_element_by_xpath('/html/body/div[1]/article/div/section[2]/article/ul/li/dl/dd[1]') button.click() time.sleep(5) #他のURL指定 driver.get("https://example.com/app/#!/zoom/123456") time.sleep(10) #BeautifulSoup指定 html = driver.page_source.encode('utf-8') soup = BeautifulSoup(html, 'html.parser') time.sleep(5) # #全てのチャットIDを検索します # found = soup.find_all(id=['chat-messages']) # print(found) # #テキストが含んでいるclass_タグを検索します # found2 = soup.find_all(class_=['selectable msg-text-box _compile ng-scope']) # print(found2) foundID = soup.find_all(class_=['selectable msg-text-box _compile ng-scope'],id=['chat-messages']) print(foundID)
fondとfond2の出力結果
# #全てのチャットIDを検索します # found = soup.find_all(id=['chat-messages']) # print(found) <div class="article-comment-input-container message-input-container _commentInputForm ng-isolate-scope" message-id="1860359798" ng-show="isWriteAbleComment" post-id="1862374024"><div class="message-input-wrapper board-comment-creator-wrap" ng-class="{'focus' : isEditorMode == 'editor'}" ng-switch="" on="isEditorMode"><!-- 버튼 역활을 하며, 클릭시, 아래의 입력창을 노출 합니다. --><!-- ngSwitchWhen: button --><!-- ngIf: isWriteAbleComment --><div class="message-input-inner comment-input trigger-input ng-binding ng-scope" ng-click="onModeEditor()" ng-if="isWriteAbleComment" ng-switch-when="button" style="">ここにメッセージを記入 (Shift + エンターキーでテキストボックス内で改行)</div><!-- end ngIf: isWriteAbleComment --><!-- end ngSwitchWhen: --><!-- ngSwitchWhen: editor --></div></div> </div> # #テキストが含んでいるclass_タグを検索します # found2 = soup.find_all(class_=['selectable msg-text-box _compile ng-scope']) # print(found2) <!-- article-contents START --> <div class="article-contents"> <!-- 게시글 본문 --> <span class="selectable msg-text-box _compile ng-scope"><a href="http://example.com" rel="nofollow" target="_blank">ORANGE</a></span> <!-- 게시글 본문 (끝) --> </div> <!-- article-contents END --> <div class="_compile ng-scope"> </div>
追記
もう一つのORANGE HTMLになります。
<div id="section-post-1862223699" class="article-container _updatedData"> <!-- article hedaer START --> <div class="article-header _profileAnchor flex-row-box "> <!-- 사용자 프로필 컨테이너 (medium) --> <div class="user-profile-container size-large flex-fix _user _profileImage " data-id="17515244"> <!-- 사용자 프로필 이미지 --> <img class="img-user-profile" src="https://example-box.com/files-profile/f0b6e5c90870c2fb50290fd3152b7b2a?size=80" onerror="this.onerror=null;this.src='https://cdn.example.com/app/assets/images/b4084919.profile_default_240_20180830.png'" alt="user profile image"> <!-- 부재중 상태 표시 레이어 --> <div class="overlay-user-profile"> <i class="icon-svg ico-away"></i> </div> <!-- 차단된 상태 표시 레이어--> <div class="overlay-user-profile blocked"> <i class="icon-ic-block"></i> </div> </div> <div class="article-profile flex-rel ellip-1"> <div class="fn-user-name member-name selectable cursor_pointer fn-13 _profileName _user">TA bot<span class="member-state-meessage"></span></div> <div class="article-date fn-11"> <span class="ng-binding">2021/11/03 12:22</span> </div> </div> <div class="article-action flex-fix fn-16 "> <ul class="snb-menu-group board-snb"> <li class=""> <div class="board-menu-btn"> <i class="icon-ic-more _postMore _posBottom"></i> </div> </li> <li class=""> <div class="board-menu-btn"> <i class="icon-ic-star _star _star-1860206267 off"></i> </div> </li> </ul> </div> </div> <!-- article hedaer END --> <!-- article body START --> <div class="article-body"> <div class="article-title fn-16"> <span class="selectable">TEST</span> </div> <!-- article-contents START --> <div class="article-contents"> <!-- 게시글 본문 --> <span class="selectable msg-text-box _compile ng-scope">ORANGE</span>
お手数ですが、よろしくお願い致します。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/11/04 11:44 編集
2021/11/04 11:51
2021/11/04 12:01
2021/11/04 12:17
2021/11/04 12:26
2021/11/09 03:49 編集
2021/11/09 03:52 編集
2021/11/10 00:11 編集