[英]I'm trying to use a Python web scraper to pull text from a page that has that text update periodically
我首先要說我是一個完全的新手,幾乎不知道自己在做什么。
我正在嘗試使用 web 刮刀從頁面中提取 4 對數字,在單獨的頁面上修改時數字會發生變化(我無法使用該頁面從中提取數據)並將其發送到我電腦上的文本文檔. 當我拉動 html 源時,文本根本不存在(沒有可以更新或更改的元素)。
使用檢查元素查看它時看起來像這樣<div id="openpages"> ==$0 <span class="thisnotes"> ***example text***</span> <div/>
這是在查看頁面源代碼時,或者將信息拉入 pycharm <div id='openpages'></div>
同樣,我真的不知道自己在做什么,但我認為這是因為該頁面使用<span class="thisnotes">
知道何時需要更新不是合適的地方。 任何和所有的幫助將不勝感激!
檢查 DOM 通常會發現一些問題。 你不能使用“find_element_by_class_name”嗎?
可以鏈接到網站嗎?
編輯:我能夠使用此代碼獲取文本:編輯:為清楚起見完整代碼:
#! python3
# These two lines import Selenium webdriver and Options
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
# These lines tell browser open links in new tabs (personal preference)
opts = Options()
opts.set_preference("browser.link.open_newwindow.restriction", 0)
opts.set_preference("browser.link.open_newwindow", 3)
# This line defines the browser
browser = webdriver.Firefox(options=opts)
# This line loads the website
browser.get('https://app.roll20.net/campaigns/journal/6581887/handout/-MAhsToCYmASlnZzZJST')
# These lines are explained below
digits = browser.find_element_by_class_name('thisnotes')
output = digits.get_attribute('innerText')
print(output)
此代碼通過 class 名稱查找元素,並將其分配給我命名為“digits”的變量。 然后我們使用“get_attribute”獲取“innerText”屬性中的任何內容,並將其分配給我命名為“output”的變量。
如果您查看 DOM 屬性,這將更有意義。 為此,您右鍵單擊(或在 OSX 上按住 Control 單擊)元素(站點上的那些數字)並選擇“檢查元素”。 它將向您顯示 HTML 並突出顯示跨度 class。 然后右鍵單擊或按住 Control 單擊它並選擇“顯示 DOM 屬性”。 一旦我在那里,它會告訴我 class name = "thisnotes"
不過,我通常不使用 class_name,更常見的是 xpath 或 css_selector。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.