# 标题list_title = driver.find_elements_by_xpath(‘//*[@id="share-content"]/div/div[1]/ul/li/h3/a‘)print(list_title)# 用selenium + xpath 抓取网页数据,上述代码返回的是列表类型,打印出来是:[<selenium.webdriver.remote.webelement.WebElement (session="35b2fa1b1832c956eb1f9d06cc3a1997", element="0.02111220449086315-1")>, <selenium.webdriver.remote.webelement.WebElement (session="35b2fa1b1832c956eb1f9d06cc3a1997", element="0.02111220449086315-2")>, <selenium.webdriver.remote.webelement.WebElement (session="35b2fa1b1832c956eb1f9d06cc3a1997", element="0.02111220449086315-3")>, <selenium.webdriver.remote.webelement.WebElement (session="35b2fa1b1832c956eb1f9d06cc3a1997", element="0.02111220449086315-4")>, <selenium.webdriver.remote.webelement.WebElement (session="35b2fa1b1832c956eb1f9d06cc3a1997", element="0.02111220449086315-5")>, <selenium.webdriver.remote.webelement.WebElement (session="35b2fa1b1832c956eb1f9d06cc3a1997", element="0.02111220449086315-6")>, <selenium.webdriver.remote.webelement.WebElement (session="35b2fa1b1832c956eb1f9d06cc3a1997", element="0.02111220449086315-7")>, <selenium.webdriver.remote.webelement.WebElement (session="35b2fa1b1832c956eb1f9d06cc3a1997", element="0.02111220449086315-8")>, <selenium.webdriver.remote.webelement.WebElement (session="35b2fa1b1832c956eb1f9d06cc3a1997", element="0.02111220449086315-9")>, <selenium.webdriver.remote.webelement.WebElement (session="35b2fa1b1832c956eb1f9d06cc3a1997", element="0.02111220449086315-10")>, <selenium.webdriver.remote.webelement.WebElement (session="35b2fa1b1832c956eb1f9d06cc3a1997", element="0.02111220449086315-11")>, <selenium.webdriver.remote.webelement.WebElement (session="35b2fa1b1832c956eb1f9d06cc3a1997", element="0.02111220449086315-12")>, <selenium.webdriver.remote.webelement.WebElement (session="35b2fa1b1832c956eb1f9d06cc3a1997", element="0.02111220449086315-13")>, <selenium.webdriver.remote.webelement.WebElement (session="35b2fa1b1832c956eb1f9d06cc3a1997", element="0.02111220449086315-14")>, <selenium.webdriver.remote.webelement.WebElement (session="35b2fa1b1832c956eb1f9d06cc3a1997", element="0.02111220449086315-15")>]
for i in list_title: print(type(i)) 显示结果:<class ‘selenium.webdriver.remote.webelement.WebElement‘> 抓取到的信息不能以中文形式显示出来,一时间陷入了怎么去编码解码解决这类型的代码,在网上找了好多方法去试,结果都报错,翻看笔记,原来这个方法很简单!!
for i in list_title: print(i.text)把列表遍历,.text方法即可得到想要的文本内容。与xpath中的text()又不一样。 1.从定位到的元素中获取数据
el.get_attribute(key) 获取key属性名对应的属性值
el.text 获取开闭标签之间的文本内容
1.对定位到的元素的操作
el.click() 对元素执行点击操作
el.submit() 对元素执行提交操作
el.clear() 清空可输入元素中的数据
el.send_keys(data) 向可输入元素输入数据
原文地址:https://www.cnblogs.com/lutt/p/10181799.html
时间: 2024-10-10 11:04:49