r - 使用 RSelenium 获取网页中的所有 Twitter 链接



我正在尝试从带有 Rselenium 的网页中收集 URL,但收到 InvalidSelector 错误

在 Windows 10 PC 上使用 R 3.6.0,使用 Chrome 网络驱动程序的 Rselenium 1.7.5 (chromever="75.0.3770.8")


library(RSelenium)
rD <- rsDriver(browser=c("chrome"), chromever="75.0.3770.8")
remDr <- remoteDriver(port = 4567L, browserName = "chrome")
remDr$open()
url <- "https://www.aph.gov.au/Senators_and_Members/Parliamentarian_Search_Results?q=&mem=1&par=1&gen=0&ps=96"
remDr$navigate(url)
tt <- remDr$findElements(using = "xpath", "//a[contains(@href,'http://twitter.com/')]/@href")

我希望收集列出的政治家的Twitter帐户的URL。 相反,我收到下一个错误:

硒信息:

invalid selector: The result of the xpath expression "//a[contains(@href,'http://twitter.com/')]/@href" is: [object Attr]. It should be an element.
(Session info: chrome=75.0.3770.80)
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/invalid_selector_exception.html
Build info: version: '4.0.0-alpha-1', revision: 'd1d3728cae', time: '2019-04-24T16:15:24'
System info: host: 'ALEX-DELL-17', ip: '10.0.75.1', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_191'
Driver info: driver.version: unknown

错误:摘要:无效选择器 细节:参数是一个无效的选择器(例如XPath/CSS)。 class: org.openqa.selenium.InvalidSelectorException 更多详细信息:运行错误详细信息方法

当我对非常具体的元素进行类似的搜索时,一切正常,例如:

tt <- remDr$findElement(value = '//a[@href = "http://twitter.com/AlboMP"]')

然后

tt$getElementAttribute('href') 

返回我需要的网址

我做错了什么?

此错误消息...

invalid selector: The result of the xpath expression "//a[contains(@href,'http://twitter.com/')]/@href" is: [object Attr]. It should be an element.

......意味着您的 XPath 表达式不是有效的表达式。

xpath 表达式:

//a[contains(@href,'http://twitter.com/')]/@href

不返回元素。它将返回一个[object Attr].虽然使用Selenium RC这是可以接受的,但WebDriver的WebElement接口的方法需要一个元素对象,而不仅仅是任何DOM节点对象。

总而言之,Selenium仍然不支持这种格式。 要解决此问题,您需要更改 HTML 标记以将文本节点包装在元素内,例如 .


溶液

要解决此问题,您需要使用findElements并创建一个列表

findElements(value = '//a[@href = "http://twitter.com/AlboMP"]')

现在,您可以循环访问列表并使用getElementAttribute('href')方法提取 URL。


参考

无效选择器错误: xpath 表达式的结果是:[对象文本]

我什么都不知道,所以我用python发布了一个答案。由于这篇文章是关于R的,所以我学习了一些R基础知识并发布了它。

获取 Twitter URL 的最简单方法是遍历网页中的所有 URL,并检查其中是否包含"twitter"一词。

在python中(工作绝对正常):

driver.get('https://www.aph.gov.au/Senators_and_Members/Parliamentarian_Search_Results?q=&mem=1&par=1&gen=0&ps=96')
links = driver.find_elements_by_xpath("//a[@href]")
for link in links:
if 'twitter' in link.get_attribute("href"):
print(link.get_attribute("href")

结果:


http://twitter.com/AlboMP http://twitter.com/SharonBirdMP http://twitter.com/Bowenchris http://twitter.com/tony_burke
http://twitter.com/lindaburneymp http://twitter.com/Mark_Butler_MP
https://twitter.com/terrimbutler http://twitter.com/AnthonyByrne_MP
https://twitter.com/JEChalmers http://twitter.com/NickChampionMP
https://twitter.com/LMChesters http://twitter.com/JasonClareMP
https://twitter.com/SharonClaydon
https://www.twitter.com/LibbyCokerMP
https://twitter.com/JulieCollinsMP http://twitter.com/fitzhunter
http://twitter.com/stevegeorganas https://twitter.com/andrewjgiles
https://twitter.com/lukejgosling https://www.twitter.com/JulianHillMP http://twitter.com/stephenjonesalp https://twitter.com/gedkearney
https://twitter.com/MikeKellyofEM http://twitter.com/mattkeogh
http://twitter.com/PeterKhalilMP http://twitter.com/CatherineKingMP
https://twitter.com/MadeleineMHKing https://twitter.com/ALEIGHMP
https://twitter.com/RichardMarlesMP
https://twitter.com/brianmitchellmp
http://twitter.com/#!/RobMitchellMP
http://twitter.com/ShayneNeumannMPhttps://twitter.com/ClareONeilMP
http://twitter.com/JulieOwensMP
http://www.twitter.com/GrahamPerrettMP
http://twitter.com/tanya_plibersek http://twitter.com/AmandaRishworth
http://twitter.com/MRowlandMP https://twitter.com/JoanneRyanLalor http://twitter.com/billshortenmp http://www.twitter.com/annewerriwa http://www.twitter.com/stemplemanmp

https://twitter.com/MThistlethwaite http://twitter.com/MariaVamvakinou https://twitter.com/TimWattsMP

https://twitter.com/joshwilsonmp

在R中:(这可能是错误的,但你可以得到一个想法)

library(XML)
library(RCurl)
library(RSelenium)
url <- "https://www.aph.gov.au/Senators_and_Members/Parliamentarian_Search_Results?q=&mem=1&par=1&gen=0&ps=96"
doc <- getURL(url)
parser <- htmlParse(doc)
links <- xpathSApply(parser, "//a[@href]", xmlGetAttr, "href")
for(link in links){
if(grepl("twitter", link)){
print(link)
}
}

我什至不知道这段代码是否有效。但是这个想法是获取页面中的所有URL,迭代它并检查其中是否有twitter这个词。 我的回答是基于这个

好吧,也许有点晚了。但是您的解决方案可能是以这种方式获取链接向量:

links=RemDr$findElements(value = "//*[contains(@href, 'https://www.twitter.com/')]")

最新更新