定位隐藏在网页中的文本
Locating the text buried in a webpage
我确实找到了很多和我相似的帖子。但是我尝试了很多建议,但似乎没有任何效果:(
我要抓取的网页是
https://m.livesoccertv.com/match/3018992/wolverhampton-wanderers-vs-arsenal/
编辑:频道列在国际报道下拉列表中
我希望在网页上显示准确的频道,并在下一行中列出部分频道
由于我在列表中的文本不完全匹配,我似乎无法 "contains" 工作
我知道我错过了一个小调整,但这就是我被困的地方。我的变量 t 阻碍了我。该网页正在使用移动设备 link 以获取较少的页面信息
这是我的代码:
desiredChannels = ['beIN Sports HD', 'BT Sport', 'Sky Sports' 'ESPN']
channelList = []
t = '//div[contains(@class="fll b_channel_name -broadcast b_trim_inner")]'
for i in range(len(desiredChannels)):
temp = desiredChannels[i]
search = browser.find_element_by_xpath(t).text
if temp in search:
channelList.append(search)
print(channelList)
感谢任何帮助
我认为您的 class 名称中的空格导致了问题。尝试以下
t = "div.fll.b_channel_name.-broadcast.b_trim_inner"
for i in range(len(desiredChannels)):
temp = desiredChannels[i]
search = browser.driver.find_element_by_css_selector(t).text
if temp in search:
channelList.append(search)
print(channelList)
首先你的Xpath
有点wrong.So我修改了一下
其次,您要查找的元素是 hidden
,因此您需要使用 javaScript executor
来获取文本。
第三,你总共有 79 个具有相同 classname
的元素,所以我没有先计数然后循环遍历 element.then 只有你会得到想要的输出。
第四个我添加了 webdriverwait
以防 webdriver
需要更长的时间来识别元素。
您需要具备以下 imports
才能使用代码。
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
browser = webdriver.Chrome()
browser.get('https://m.livesoccertv.com/match/3018992/wolverhampton-wanderers-vs-arsenal/')
browser.maximize_window()
desiredChannels = ['beIN Sports HD', 'BT Sport', 'Sky Sports', 'ESPN']
channelList = []
t = '//div[@class="fll b_channel_name -broadcast b_trim_inner"]'
#channels=browser.find_elements_by_xpath(t)
channels=WebDriverWait(browser, 30).until(EC.presence_of_all_elements_located((By.XPATH, t)))
for search in channels:
searchtext=browser.execute_script("return arguments[0].innerHTML;", search)
for i in range(len(desiredChannels)):
temp = desiredChannels[i]
if temp in searchtext:
channelList.append(searchtext)
print(channelList)
输出:
['Watch ESPN Brasil']
希望对您有所帮助。
1) 您应该使用 find_elements_xxxx()
来获取所有频道,而不是 find_element_xxx()
只 return 第一个匹配的频道。
2) 您需要点击 'International Coverage' 使频道可见,
.text
return 不可见元素上的空字符串。
desiredChannels = ['beIN Sports HD', 'BT Sport', 'Sky Sports', 'ESPN']
channelList = []
// collapse toggle
collapse_toggle = driver.find_element_by_css_selector('div.int_coverage > a[data-toggle]')
// expand all channels to make them visible, otherwise `.text` return empty string on invisible element
if collapse_toggle.get_attribute('class') == 'collapsed':
collapse_toggle.click();
channels = driver.find_elements_by_css_selector('div.int_coverage + div div.b_channel_name:not(.-country)')
for channel in channels:
channelName = channel.text
for desiredChannel in desiredChannels:
if channelName.find(desiredChannel) > -1:
channelList.append(channelName)
break
我确实找到了很多和我相似的帖子。但是我尝试了很多建议,但似乎没有任何效果:(
我要抓取的网页是 https://m.livesoccertv.com/match/3018992/wolverhampton-wanderers-vs-arsenal/
编辑:频道列在国际报道下拉列表中
我希望在网页上显示准确的频道,并在下一行中列出部分频道
由于我在列表中的文本不完全匹配,我似乎无法 "contains" 工作
我知道我错过了一个小调整,但这就是我被困的地方。我的变量 t 阻碍了我。该网页正在使用移动设备 link 以获取较少的页面信息
这是我的代码:
desiredChannels = ['beIN Sports HD', 'BT Sport', 'Sky Sports' 'ESPN']
channelList = []
t = '//div[contains(@class="fll b_channel_name -broadcast b_trim_inner")]'
for i in range(len(desiredChannels)):
temp = desiredChannels[i]
search = browser.find_element_by_xpath(t).text
if temp in search:
channelList.append(search)
print(channelList)
感谢任何帮助
我认为您的 class 名称中的空格导致了问题。尝试以下
t = "div.fll.b_channel_name.-broadcast.b_trim_inner"
for i in range(len(desiredChannels)):
temp = desiredChannels[i]
search = browser.driver.find_element_by_css_selector(t).text
if temp in search:
channelList.append(search)
print(channelList)
首先你的Xpath
有点wrong.So我修改了一下
其次,您要查找的元素是 hidden
,因此您需要使用 javaScript executor
来获取文本。
第三,你总共有 79 个具有相同 classname
的元素,所以我没有先计数然后循环遍历 element.then 只有你会得到想要的输出。
第四个我添加了 webdriverwait
以防 webdriver
需要更长的时间来识别元素。
您需要具备以下 imports
才能使用代码。
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
browser = webdriver.Chrome()
browser.get('https://m.livesoccertv.com/match/3018992/wolverhampton-wanderers-vs-arsenal/')
browser.maximize_window()
desiredChannels = ['beIN Sports HD', 'BT Sport', 'Sky Sports', 'ESPN']
channelList = []
t = '//div[@class="fll b_channel_name -broadcast b_trim_inner"]'
#channels=browser.find_elements_by_xpath(t)
channels=WebDriverWait(browser, 30).until(EC.presence_of_all_elements_located((By.XPATH, t)))
for search in channels:
searchtext=browser.execute_script("return arguments[0].innerHTML;", search)
for i in range(len(desiredChannels)):
temp = desiredChannels[i]
if temp in searchtext:
channelList.append(searchtext)
print(channelList)
输出:
['Watch ESPN Brasil']
希望对您有所帮助。
1) 您应该使用 find_elements_xxxx()
来获取所有频道,而不是 find_element_xxx()
只 return 第一个匹配的频道。
2) 您需要点击 'International Coverage' 使频道可见,
.text
return 不可见元素上的空字符串。
desiredChannels = ['beIN Sports HD', 'BT Sport', 'Sky Sports', 'ESPN']
channelList = []
// collapse toggle
collapse_toggle = driver.find_element_by_css_selector('div.int_coverage > a[data-toggle]')
// expand all channels to make them visible, otherwise `.text` return empty string on invisible element
if collapse_toggle.get_attribute('class') == 'collapsed':
collapse_toggle.click();
channels = driver.find_elements_by_css_selector('div.int_coverage + div div.b_channel_name:not(.-country)')
for channel in channels:
channelName = channel.text
for desiredChannel in desiredChannels:
if channelName.find(desiredChannel) > -1:
channelList.append(channelName)
break