gpt4 book ai didi

python-3.x - 定位隐藏在网页中的文本

转载 作者:行者123 更新时间:2023-12-01 14:58:39 25 4
gpt4 key购买 nike

我确实发现了很多与我相似的帖子。但我已经尝试了很多建议,但似乎没有任何效果:(

我要抓取的网页是 https://m.livesoccertv.com/match/3018992/wolverhampton-wanderers-vs-arsenal/

编辑: channel 列在国际覆盖下拉菜单下

我希望通过以下行中包含的部分 channel 列表获得网页上显示的确切 channel

由于我在列表中的文本不是完全匹配的,我似乎无法让“包含”起作用

我知道我错过了一个小调整,但这就是我被困的地方。我的变量 t 阻碍了我。该网页正在使用移动链接获取较少的页面信息

这是我的代码:

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)

感谢任何帮助

最佳答案

首先你的Xpath有点不对,所以我修改了一下。

其次,您要查找的元素是隐藏的,因此您需要使用javaScript executor 来获取文本。

第三,你总共有 79 个具有相同 classname 的元素,所以我先进行计数,然后循环遍历元素。然后只有你会得到所需的输出。

第四,我添加了 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']

希望对你有帮助。

关于python-3.x - 定位隐藏在网页中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55112059/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com