Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/405.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用RSelenium获取网页中的所有twitter链接_R_Selenium_Xpath_Rselenium_Xpath 1.0 - Fatal编程技术网

使用RSelenium获取网页中的所有twitter链接

使用RSelenium获取网页中的所有twitter链接,r,selenium,xpath,rselenium,xpath-1.0,R,Selenium,Xpath,Rselenium,Xpath 1.0,我试图用Rselenium从网页收集URL,但出现InvalidSelector错误 在Windows 10 PC上使用R3.6.0,Rselenium 1.7.5和Chrome webdriver(chromever=“75.0.3770.8”) 错误:摘要:InvalidSelector 详细信息:参数是无效的选择器(例如XPath/CSS)。 类别:org.openqa.selenium.InvalidSelectorException 更多详细信息:运行errorDetails方法 当我

我试图用Rselenium从网页收集URL,但出现InvalidSelector错误

在Windows 10 PC上使用R3.6.0,Rselenium 1.7.5和Chrome webdriver(chromever=“75.0.3770.8”)

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

当我对非常特定的元素进行类似的搜索时,一切都很好,例如:

tt <- remDr$findElement(value = '//a[@href = "http://twitter.com/AlboMP"]')
返回我需要的URL

我做错了什么?

此错误消息

invalid selector: The result of the xpath expression "//a[contains(@href,'http://twitter.com/')]/@href" is: [object Attr]. It should be an element.
……表示您的XPath表达式无效

表达方式:

//a[contains(@href,'http://twitter.com/')]/@href
不返回元素。它将返回一个
[object Attr]
。虽然使用
Selenium RC
这是可以接受的,但是WebDriver的WebElement接口的方法需要一个element对象,而不仅仅是任何DOM节点对象

总之,仍然不支持这种格式。为了解决这个问题,您需要更改HTML标记,将文本节点包装到元素中,如


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

现在,您可以迭代列表,并使用
getElementAttribute('href')
方法提取URL


参考文献
我对R一无所知,所以我用python发布了一个答案。因为这篇文章是关于R的,所以我学习了一些R的基本知识,并将其发布

获取twitter URL最简单的方法是迭代网页中的所有URL,并检查其中是否包含“twitter”一词

在python中(它工作得非常好):

结果:


























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

库(XML)
图书馆(RCurl)
图书馆(资源库)

url当我检查站点时,没有包含此类xpath的元素。你能用css_选择器或链接文本试试吗?代码应该查找到twitter的所有链接,它假设找到所有包含严格搜索条件的底部代码(具体的twitter URL)的URL就可以了。也许问题在于软xpath语法,但我看不出哪里出了问题。嗨,Debanjan,谢谢你的回答
我知道代码`findElements(value='//a[@href=“)`works.
但是它并不能解决我的任务-我需要提取我不知道的URL,但要匹配特定的模式,即在URL中包含'twitter.com'。因此我使用带有'contain'的表达式。
上面的代码只返回一个URL。@Alex您看到的错误
无效选择器
和提取URL的任务是完全不同的两个方面。在这个答案中,我为您提供了关于如何避免
无效选择器
错误的规范答案。您的答案有效地重复了我问题的最后一部分,我已经知道代码findElements(value='//a[@href=)workedThanks@Prasanth!这种方法,使用“xpathsaply(parser,”//a[@href]”,xmlGetAttr,“href”)“可以查找页面上的所有URL,然后确实可以对这些URL进行过滤,以仅包含匹配条件的子集。
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.
//a[contains(@href,'http://twitter.com/')]/@href
findElements(value = '//a[@href = "http://twitter.com/AlboMP"]')
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")
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)
    }
}