- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
背景我目前正在使用 rvest 从 R 中的一些网站抓取产品信息。这适用于除一个网站之外的所有网站,其中内容似乎是通过 angularJS(?)动态加载的,因此无法迭代加载,例如通过 URL 参数(就像我对其他网站所做的那样)。具体网址如下:
http://www.hornbach.de/shop/Badarmaturen/Waschtischarmaturen/S3584/artikelliste.html
请记住,我的计算机没有管理员权限,只能实现不需要或仅一次性授予管理员权限的解决方案
所需输出最后,R 中的表格包含产品信息(例如标签、价格、评级)=> 不过,在这个问题中,我纯粹需要帮助来动态加载和存储网站;我可以自己处理 R 中的后处理。如果你能把我推向正确的方向,那就太好了;也许下面列出的我的方法之一是正确的,但我似乎无法将这些方法转移到指定的网站。
当前方法我发现 phantomJS 作为一个 headless 浏览器,据我所知应该能够处理这个问题。我对 Java 脚本几乎一无所知,而且语法与我更习惯的语言(R、Matlab、SQL)有很大不同(至少对我来说),我真的很难实现其他地方建议的可能适用的方法我的代码。基于this example (非常感谢)我设法使用以下代码从显示的第一个页面至少检索信息:
R:
require(rvest)
## change Phantom.js scrape file
url <- 'http://www.hornbach.de/shop/Badarmaturen/Waschtischarmaturen/S3584/artikelliste.html'
lines <- lines <- readLines("scrape_final.js")
lines[1] <- paste0("var url ='", url ,"';")
writeLines(lines, "scrape_final.js")
## Download website
system("phantomjs scrape_final.js")
### use Rvest to scrape the downloaded website.
web <- read_html("1.html")
content <- html_nodes(web, 'div.paging-indicator')# %>% html_attr('href')
content <- html_text(content) %>% as.data.frame()
以及相应的PhantomJS脚本:
var url ='http://www.hornbach.de/shop/Badarmaturen/Waschtischarmaturen/S3584/artikelliste.html';
var page = new WebPage()
var fs = require('fs');
page.open(url, function (status) {
just_wait();
});
function just_wait() {
setTimeout(function() {
fs.write('1.html', page.content, 'w');
phantom.exit();
}, 2500);
}
什么不起作用//研究虽然此代码从第一页检索信息,但我确实需要遍历所有 x 个产品页面。我尝试用以下示例扩展上面的代码:
Unable to scrape multiple pages using phantomjs in r
[使用phantomjs抓取动态加载页面][3]
[R 中的网页抓取动态加载数据][4]
这些例子让我想到了这个想法
或者以某种方式注入(inject)正确的分页值
看起来像下面这样
var url ='http://www.hornbach.de/shop/Badarmaturen/Waschtischarmaturen/S3584/artikelliste.html';
var page = require('webpage').create();
var fs = require('fs');
page.open(url, function (status) {
age.open(url, function() {
page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
page.evaluate(function() {
$("paging-btn right").click();
just_wait();
});
phantom.exit()
});
});
function just_wait() {
setTimeout(function() {
fs.write('1.html', page.content, 'w');
phantom.exit();
}, 2500);
}
但是由于语法不佳以及其他原因,这对我没有任何帮助。不幸的是,从 R 调用这个脚本不会产生错误,它只是运行了很长时间,所以我必须退出它(而工作脚本只需要几秒钟)。
我使用 Firefox 中的小工具检查器来检索按钮名称,但这也可能是错误的:
<a class="paging-btn right rel="next" ng-click="goToNextPage()"
ng-hide="articleData.pageNumber == articleData.pageCount"
href="javascript:void(0);">right</a>
我尝试处理此处给出的示例 Passing variable into page.evaluate - PhantomJS
但也刚刚得到了一个从未以 R 结尾的脚本
附加说明看起来我只能发布两个链接,所以不幸的是我无法链接我研究和测试过的所有来源。
我很清楚这是一次巨大而困惑的信息,如果您可以帮助我改进/更好地构建我的问题,请告诉我。我会尽力做出回应并为您提供任何您需要的帮助。
最佳答案
我将 PhantomJS 代码分成两部分,以避免出现错误消息。我非常有信心可以先读取并存储网站,然后点击“下一页”按钮并输出新的网址,但不幸的是,这并没有在没有错误消息的情况下成功。
以下R代码是最内部的抓取循环(从一个子子类别的页面检索信息,相应地调用/更改PhantomJS脚本)。
for (i3 in 1:num_prod_pages) {
system('phantomjs readhtml.js') # return html of current page via PhantomJS
### Use Rvest to scrape the downloaded website.
html_filename <- paste0(as.character(i3), '.html') # file generated in PhantomJS readhtml.js
web <- read_html(html_filnamee)
content <- html_nodes(web, 'div.article-pricing') # %>% html_attr('href')
content <- html_text(content) %>% as.data.frame()
### generate URL of next page
url_i3 <- capture.output(system("phantomjs nextpage.js", intern = TRUE)) %>%
.[length(.)] %>% # last line of output contains
str_sub(str_locate(., 'http')[1], -2) # cut '[1] \' at start and ' \" ' at end
# Adapt PhantomJS scripts to new url
lines <- readLines("readhtml.js")
lines[2] <- paste0("var url ='", url_i3 ,"';")
lines[11] <- paste0(" fs.write('", as.character(i3), ".html', page.content, 'w');")
writeLines(lines, "readhtml.js")
lines <- readLines("nextpage.js")
lines[2] <- paste0("var url ='", url_i3 ,"';")
writeLines(lines, "nextpage.js")
}
以下PhantomJS代码“readhtml.js”代码在本地存储具有当前URL的网站
var webPage = require('webpage');
var url ='http://www.hornbach.de/shop/Badarmaturen/S476/artikelliste.html';
var fs = require('fs');
var page = webPage.create();
var system = require('system');
//page.settings.userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0'
page.settings.userAgent = 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)'
page.open(url, function(status) {
if (status === 'success') {
fs.write('1.html', page.content, 'w');
console.log('htmlfile ready');
phantom.exit();
}
})
下面的PhantomJS代码“nextpage.js”代码点击“下一页”按钮并返回新的URL
var webPage = require('webpage');
var url ='http://www.hornbach.de/shop/Badarmaturen/S476/artikelliste.html';
var fs = require('fs');
var page = webPage.create();
var system = require('system');
page.settings.userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0';
page.open(url, function(status) {
if (status === 'success') {
page.evaluate(function() {
document.querySelector('a.right:nth-child(3)').click();
});
setTimeout(function() {
var new_url = page.url;
console.log('URL: ' + new_url);
phantom.exit();
}, 2000);
};
});
总而言之,不是很优雅,但缺乏其他输入,我关闭了这个,因为它工作时没有任何错误消息
关于jquery - 在 R 中使用 phantomJS 抓取具有动态加载内容的页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46112254/
如本answer所述,如果浏览器不支持 e,可以设置后备游标。 G。 光标:抓取;。我现在的问题是获取这些图像。在我的驱动器上本地搜索“.cur”只给了我系统光标,其中 grab.cur 和 grab
以下代码在计算机上运行以从 Instagram 帐户中抓取数据。当我尝试在 VPS 服务器上使用它时,我被重定向到 Instagram 登录页面,因此脚本不起作用。 为什么当我在电脑上或服务器上时,I
我在使用 Ruby 和 Mechanize 将 POST 查询传递到站点的网站上。访问站点的查询基于 firebug,如下所示 param.PrdNo=-1¶m.Type=Prop¶m
我正在尝试抓取一个具有多个页面结果的网站,例如“1、2、3、4、5...”。 每个分页号都是到另一个页面的链接,我需要抓取每个页面。 到目前为止,我想出了这个: while lien = page.l
我正在使用 HtmlAgilityPack 在 C# Asp.Net 中执行 Scraping,到目前为止,我在从多个 Web 执行 Scratch 时没有遇到问题,但是,尝试弹出以下代码时出现错误
如果我有一个 css 文件做这样的事情 #foo:after{content:"bar;} ,有没有办法用 javascript 获取 :after 的内容?获取父元素的内容只返回 #foo 元素的内
问题是这样的: 我有一个 Web 应用程序 - 一个经常更改的通知系统 - 在一系列本地计算机上运行。该应用程序每隔几秒刷新一次以显示新信息。计算机仅显示信息,没有键盘或任何输入设备。 问题是,如果与
我想制作一个程序来模拟用户浏览网站和点击链接。必须启用 Cookie 和 javascript。我已经在 python 中成功地做到了这一点,但我想把它写成一种可编译的语言(python ide 不会
我制作了这个小机器人,它通过搜索参数列表进行处理。它工作正常,直到页面上有几个结果: product_prices_euros 给出了一半为空的项目列表。因此,当我与 product_prices_c
我需要找到一个单词的匹配项,例如: 在网上找到所有单词“学习”https://www.georgetown.edu/(结果:4个字)(您可以看到它按CTRL + F并搜索) 我有我的 Python 代
有一个站点\资源提供一些一般统计信息以及搜索工具的界面。这种搜索操作成本高昂,因此我想限制频繁且连续(即自动)的搜索请求(来自人,而不是来自搜索引擎)。 我相信有很多现有的技术和框架可以执行一些情报抓
这并不是真正的抓取,我只是想在网页中找到类具有特定值的 URL。例如: 我想获取 href 值。关于如何做到这一点的任何想法?也许正则表达式?你能发布一些示例代码吗?我猜 html 抓取库,比如 B
我正在使用 scrapy。 我正在使用的网站具有无限滚动功能。 该网站有很多帖子,但我只抓取了 13 个。 如何抓取剩余的帖子? 这是我的代码: class exampleSpider(scrapy.
我正在尝试从这个 website 中抓取图像和新闻 url .我定义的标签是 root_tag=["div", {"class":"ngp_col ngp_col-bottom-gutter-2 ng
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭上个月。 Improve this ques
我在几个文件夹中有数千个 html 文件,我想从评论中提取数据并将其放入 csv 文件中。这将允许我为项目格式化和清理它。例如,我在这个文件夹中有 640 个 html 文件: D:\My Web S
我在编写用于抓取网页的实用程序时遇到了一个问题。 我正在发送 POST 请求来检索数据,我模仿我正在抓取的网络行为(根据使用 fiddler 收集的信息)。 我已经能够自动替换我的 POST 中除 V
对于 Googlebot 的 AJAX 抓取,我在我的网站中使用“_escaped_fragment_”参数。 现在我查看了 Yandex 对我网站的搜索结果。 我看到搜索结果中不存在 AJAX 响应
我正在尝试抓取网站的所有结果页面,它可以工作,但有时脚本会停止并显示此错误: 502 => Net::HTTPBadGateway for https://website.com/id/12/ --
我是一个学习网络爬虫的初学者,由于某种原因我无法爬网this地点。当我在 Chrome 中检查它时,代码看起来不错,但是当我用 BeautifulSoup 阅读它时,它不再是可刮的。汤提到“谷歌分析”
我是一名优秀的程序员,十分优秀!