回答編集履歴

1

コードを追加

2022/06/24 11:20

投稿

meg_
meg_

スコア10602

test CHANGED
@@ -22,3 +22,29 @@
22
22
  element.click()
23
23
  sleep(3)
24
24
  print(driver.page_source)
25
+ ```
26
+
27
+ ---
28
+ コメントを受けて追記。
29
+ ```Python
30
+ topic = input()
31
+
32
+ chrome_service = fs.Service(executable_path='chromedriver.exe')
33
+ driver = webdriver.Chrome(service=chrome_service)
34
+ driver.get('https://news.yahoo.co.jp')
35
+ form = driver.find_element(By.XPATH,'//*[@id="searchBoxWrap-header"]/form/div/div/div[1]/input[1]')
36
+ form.send_keys(topic)
37
+ form.submit()
38
+ top_url = driver.current_url
39
+ top_responce = requests.get(top_url)
40
+ top_soup = BeautifulSoup(top_responce.text, 'html.parser')
41
+ with open("1.txt", "w", encoding="utf-8") as f:
42
+ f.write(top_soup.prettify())
43
+ element = driver.find_element(By.XPATH,'//*[@id="contentsWrap"]/div/div[2]/div/div/span/button')
44
+ element.click()
45
+ sleep(3)
46
+ with open("2.txt", "w", encoding="utf-8") as f:
47
+ f.write(BeautifulSoup(driver.page_source, 'html.parser').prettify())
48
+ # f.write(driver.page_source)
49
+ ```
50
+ 当方の実行結果では1.txtは3434行で、2.txtは6184行となりました。よってデータを取得できていると思うのですが質問者さんの方では``element.click()``の前後で全く同じテキストしか取得できないのでしょうか?