在Python、Selenium Webdri中提取<a>内容

2024-05-18 12:04:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我实际上做了一个脚本,检查拍卖门户网站为我感兴趣的新拍卖。现在脚本选择项目名称、类别、添加时间并列出拍卖清单。这是开始我的问题。我的代码:

#List of auctions
time.sleep(2)
lists= driver.find_elements_by_class_name("vela__item__1FnoI")
print ("Found " + str(len(lists)) + " auctions")

for link in driver.find_elements_by_xpath('//div[@class="vela__item__1FnoI"]//a'):
    print (link.get_attribute('href') + "-" + link.text)

现在看起来很可怕:

请帮助我实现这个结果:

http://allegro.pl/doris-wozek-dla-lalek-3f-nosidlo-torba-posciel-15k-i6735944795.html-DORIS WÓZEK DLA LALEK 3F NOSIDŁO TORBA POŚCIEL 15K

http://allegro.pl/sukienka-ubranko-dla-lalki-barbie-de-lux-i6739976160.html-Sukienka ubranko dla lalki芭比娃娃!德勒克斯!在

HTML搜索结果:

<article class="item__item__2lO83 "> <div class="vela__item__1FnoI"> <div class="vela__item__details__1di9R"> <div class="photo__thumbnail__1SaYl "> <noscript> <i><img src="https://1.allegroimg.com/s128/0166b6/964534be46848305f499770a74f1" alt="DORIS WÓZEK DLA LALEK 3F NOSIDŁO TORBA POŚCIEL 15K" /></i> </noscript> </div> <h2 class="header__title__2RWO4"> <a href="http://allegro.pl/doris-wozek-dla-lalek-3f-nosidlo-torba-posciel-15k-i6735944795.html">DORIS WÓZEK DLA LALEK 3F NOSIDŁO TORBA POŚCIEL 15K</a> </h2> </div> </div> </article><article class="item__item__2lO83 "> <div class="vela__item__1FnoI"> <div class="vela__item__details__1di9R"> <div class="photo__thumbnail__1SaYl "> <noscript> <i><img src="https://e.allegroimg.com/s128/0129ef/ec0ceef742ce9cdecbe3465a67fe" alt="Sukienka ubranko dla lalki Barbie! DE LUX!" /></i> </noscript> </div> <h2 class="header__title__2RWO4"> <a href="http://allegro.pl/sukienka-ubranko-dla-lalki-barbie-de-lux-i6739976160.html">Sukienka ubranko dla lalki Barbie! DE LUX!</a> </h2> </div> </div> </article>

Tags: divhttphtmlarticlelinkallegroh2item
2条回答

您可以使用以下代码来提取链接和链接文本:

for link in driver.find_elements_by_xpath('//div[@class="vela__item__1FnoI "]//a'):
    print(link.get_attribute('href') + "-" + link.text)

print (item)中,您正在打印WebElementto_string()方法。要打印文本,请使用

print (item.text)

相关问题 更多 >