我正在使用Rselenium
进行剪贴。为此,我在我的Google Cloud VM实例中安装了java
和JDK's
,chromedriver
,selenium server standalone
和无头浏览器phantomjs
。
我需要赶上第一等级的文字
remDr <- remoteDriver(browserName = 'chrome', port = 4444L)
remDr$open()
remDr$setWindowSize(1280L, 1024L)
remDr$navigate("https://www.ratebeer.com/reviews/sullerica-1561/294423")
text_post = remDr$findElements("xpath",'//*[@id="root"]/div/div[2]/div/div[2]/div[2]/div/div[1]/div[3]/div/div[2]/div[1]/div/div[2]/div/div[1]/div/div/div[1]')
text_post
## list()
最后text_post
为空。
但是,如果我在本地笔记本电脑上使用RSelenium,chrome浏览器和相同的XPath测试相同的脚本,那就成功了!
这是怎么回事?
是因为使用了phantomjs吗?
谢谢。
sessionInfo()
R version 3.4.4 (2018-03-15)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.5 LTS
答案 0 :(得分:1)
您不需要重量级的第三方依赖项。该站点在异步XHR请求中在后台使用graphql POST
请求来检索数据。如果您打开开发人员工具并
我做了一个“复制POST数据”(在所有浏览器中通常是相同或非常相似的上下文菜单项),并在“响应”选项卡中最小化了graphql查询,只是为了向您展示它的含义,也许还使您可以更轻松地查看查询并自行进行查询(我刚才说的内容超出了“但是呢...”的范围,请在注释中关注问题;如果需要帮助,请提出一个新问题)。 / p>
'[
{
"operationName": "beer",
"query": "query beer($beerId: ID!) {\n info: beer(id: $beerId) {\n id\n name\n __typename\n }\n}\n",
"variables": {
"beerId": "294423"
}
},
{
"operationName": "beer",
"query": "query beer($beerId: ID!) {\n info: beer(id: $beerId) {\n id\n name\n styleScore\n overallScore\n averageRating\n ratingCount\n __typename\n }\n}\n",
"variables": {
"beerId": "294423"
}
},
{
"operationName": "beerReviews",
"query": "query beerReviews($beerId: ID!, $authorId: ID, $order: ReviewOrder, $after: ID) {\n beerReviewsArr: beerReviews(beerId: $beerId, authorId: $authorId, order: $order, after: $after) {\n items {\n ...ReviewItem\n __typename\n }\n totalCount\n last\n __typename\n }\n}\n\nfragment ReviewItem on Review {\n id\n comment\n score\n scores {\n appearance\n aroma\n flavor\n mouthfeel\n overall\n __typename\n }\n author {\n id\n username\n reviewCount\n __typename\n }\n checkin {\n id\n place {\n id\n name\n city\n state {\n id\n name\n __typename\n }\n country {\n id\n name\n __typename\n }\n __typename\n }\n __typename\n }\n servedIn\n likeCount\n likedByMe\n createdAt\n updatedAt\n __typename\n}\n",
"variables": {
"beerId": "294423",
"first": 7,
"order": "RECENT"
}
}
]' -> graphql_query
我们需要将其压缩回一行以进行API调用(我在下面的gsub()
中进行此操作。我们还需要手动指定内容类型,并确保httr
不会尝试通过将编码设置为raw
来修改正文数据:
httr::POST(
url = "https://beta.ratebeer.com/v1/api/graphql/",
httr::content_type("application/json"),
encode = "raw",
body = gsub("\n", " ", graphql_query),
httr::verbose()
) -> res
现在,我们有了一个结构化但嵌套严重的列表,其中包含您的ifo。确信您在下面的items
之后:
str(httr::content(res), 4)
## List of 3
## $ :List of 1
## ..$ data:List of 1
## .. ..$ info:List of 3
## .. .. ..$ id : chr "294423"
## .. .. ..$ name : chr "Sullerica 1561"
## .. .. ..$ __typename: chr "Beer"
## $ :List of 1
## ..$ data:List of 1
## .. ..$ info:List of 7
## .. .. ..$ id : chr "294423"
## .. .. ..$ name : chr "Sullerica 1561"
## .. .. ..$ styleScore : num 35.1
## .. .. ..$ overallScore : num 51.8
## .. .. ..$ averageRating: num 3.25
## .. .. ..$ ratingCount : int 21
## .. .. ..$ __typename : chr "Beer"
## $ :List of 1
## ..$ data:List of 1
## .. ..$ beerReviewsArr:List of 4
## .. .. ..$ items :List of 10
## .. .. ..$ totalCount: int 21
## .. .. ..$ last : chr "7177326"
## .. .. ..$ __typename: chr "ReviewList"
它只有21个中的10个,因此在打开开发人员工具的情况下在浏览器窗口中向下滚动并查看发出的第二个POST
请求,查看更改了哪些参数,现在您将有一个更好的主意如何访问网站的后端API以及如何抓取内容。
答案 1 :(得分:0)
根据HTML,您可以将 xpath 用作:
//div[@id="root"]//span[contains(.,'20')]//following::div[contains(@class,'LinesEllipsis')]
注意:由于元素是动态生成的元素,因此必须诱使 WebDriverWait 使元素可见。