相关文章推荐
大气的眼镜  ·  ImportError: ...·  1 年前    · 
魁梧的汤圆  ·  js数组转json ...·  1 年前    · 
叛逆的苦瓜  ·  Oracle Data Provider ...·  2 年前    · 

Python selenium通过xpath查找元素的多个条件

2 人不认可

我使用python selenium来做网页抓取。我想抓取有特定日期的网站(如[01-20])和有特定文字的标题(如'public'),代码如何能满足这两点?

I tried the following but no luck

提前感谢你!!!。

href:

<td width="89%" height="26"> 
<a href="hrefhref" target="_blank" title="sth sth public">sth sth public</a>
<td width="8%" align="center">[01-20]</td>

code:

titles = driver.find_elements_by_css_selector("[title*='public']")
for title in titles:
    links=[title.get_attribute('href') for title in driver.find_elements_by_xpath("//td[text()='[01-20]']/preceding::td[1]/a")]
    urls = [links.get_attribute("href") for links in driver.find_elements_by_css_selector("[title*='public']")]
    for url in urls:
        print(url)
        driver.get(url)
        ###do something