鱼C论坛

 找回密码
 立即注册
查看: 882|回复: 3

selenium中查找具体标签值的问题

[复制链接]
发表于 2021-9-2 09:32:41 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
from selenium.webdriver import Firefox
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support.select import Select
from lxml import etree
import time

opt = Options()
opt.add_argument("--headless")
opt.add_argument("--disable-gpu")

web = Firefox(options=opt)
web.get("https://www.endata.com.cn/BoxOffice/BO/Year/index.html")
ele = web.find_element_by_xpath('//*[@id="OptionDate"]')
sel = Select(ele)
for i in range(len(sel.options)):
    op = sel.select_by_index(i)
    time.sleep(2)
    table = web.find_element_by_xpath('//*[@id="TableList"]/table')

    tree = etree.HTML(table.text)
    th = tree.xpath("//thead/text()")
    trs = tree.xpath("//tbody/tr")
    print(th)
    for tr in trs:
        td = tr.xpath("//td/text()")
        print(td)
        print("==========================================")
print("运行结束!")
web.close()

这个程序是查找电影年度排行榜的,
要打印出每年电影排名的标头以及每个电影的排名,名称,类型等等。
但是我这个打印出来的都是空列表。
哪位前辈帮忙看一下啊,谢谢啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-9-2 11:00:42 | 显示全部楼层
本帖最后由 suchocolate 于 2021-9-2 13:59 编辑
from selenium.webdriver import Firefox
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support.select import Select
from lxml import etree
import time

opt = Options()
opt.add_argument("--headless")
opt.add_argument("--disable-gpu")
web = Firefox(options=opt)
web.get("https://www.endata.com.cn/BoxOffice/BO/Year/index.html")
time.sleep(10)
trs = web.find_elements_by_xpath('//tr')
movies = dict()
for tr in trs[1:]:
    rank, name, info = tr.text.split('\n')
    m_type, box, ticket, people, country, time = info.split(' ')
    movies[rank] = [name, m_type, box, ticket, people, country, time]
print(movies)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-9-2 13:40:30 | 显示全部楼层

谢谢你啊,不过可能是我没说清楚,我的意思是,在我得到table这个标签后,想把table里的东西拆分出来。
这个我不会操作。还请前辈多多指教!
还有就是得到的table.text我能打印出来,结果不是让我很满意,我想挑出几个我想要的信息打印。用lxml来解析,得出的都是空的,不知道是什么原因?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-9-2 14:28:19 | 显示全部楼层
本帖最后由 suchocolate 于 2021-9-2 14:34 编辑
江湖散人 发表于 2021-9-2 13:40
谢谢你啊,不过可能是我没说清楚,我的意思是,在我得到table这个标签后,想把table里的东西拆分出来。
...

from selenium.webdriver import Firefox
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support.select import Select
from lxml import etree
import time

opt = Options()
opt.add_argument("--headless")
opt.add_argument("--disable-gpu")
web = Firefox(options=opt)
web.get("https://www.endata.com.cn/BoxOffice/BO/Year/index.html")
time.sleep(10)
trs = web.find_elements_by_xpath('//tr')
movies = dict()
# table = trs[0] # table是列名,其数目和数据行有差别,自己决定如何处理。
for tr in trs[1:]:
    txt = tr.get_attribute("outerHTML")  # element.text获取节点的文本不包含html tag,element.get_attribute("outerHTML")获取本节点和子节点码源,element.get_attribute("innerHTML")获取子节点码源。
    html = etree.HTML(txt)
    rank = html.xpath('//tr/td[1]/text()')[0]
    name = html.xpath('//tr/td[2]/a/p/text()')[0]
    m_type = html.xpath('//tr/td[3]/text()')[0]
    box = html.xpath('//tr/td[4]/text()')[0]
    ticket = html.xpath('//tr/td[5]/text()')[0]
    people = html.xpath('//tr/td[6]/text()')[0]
    country = html.xpath('//tr/td[7]/text()')[0]
    time = html.xpath('//tr/td[8]/text()')[0]
    movies[rank] = [name, m_type, box, ticket, people, country, time]
print(movies)
个人觉得这个案例用lxml获取不高效,建议直接用我的第一种代码获取数据。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-7 16:26

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表