- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想提取有关此 link 中每个广告的信息。现在,我已经到了可以自动点击查看广告详细信息
的阶段,但有很多基础数据无法直接整理成整洁的数据框。
library(RSelenium)
rs <- rsDriver()
remote <- rs$client
remote$navigate(
paste0(
"https://www.facebook.com/ads/library/?",
"active_status=all&ad_type=political_and_issue_ads&country=US&",
"impression_search_field=has_impressions_lifetime&",
"q=actblue&view_all_page_id=38471053686"
)
)
test <- remote$findElement(using = "xpath", "//*[@class=\"_7kfh\"]")
test$clickElement()
## Manually figured out element
test <- remote$findElement(using = "xpath", "//*[@class=\"_7lq0\"]")
test$getElementText()
输出文本本身很困惑,但我相信通过一些时间和努力,它可以被整理成有用的东西。问题在于处理
中的底层数据我不知道如何系统地提取这张图像,尤其是传单 svg。在这种情况下,我将如何获取每个广告,然后提取详细信息中可用的完整数据?
最佳答案
年龄和性别图形是 Canvas 元素。要将它们作为图像获取,您可以截取元素的屏幕截图。 Python 示例:
driver.find_element_by_tag_name('canvas').screenshot("age_and_gender.png")
此广告的展示位置是 SVG,您可以用相同的方式将其另存为图像。结果不会很准确,因为 SVG 的可见部分和实际是不同的。但您可以在之后裁剪图像。 Python 示例:
driver.find_element_by_tag_name('svg').screenshot("where_this_ad_was_shown.png")
要从中提取完整数据,您不能使用 Selenium。获取数据的方式是配置代理服务器,捕获 API 请求,并获取 JSON 格式的数据。是的,这是可能的。
<小时/>简单的方法是使用一些请求来获取 AD 和详细信息,而无需使用 Selenium。 Python 工作示例:
import json
import requests
params = (
('q', 'actblue'),
('count', '1000'), # default is 30, for 38471053686 it will return about 300 results.
('active_status', 'all'),
('ad_type', 'political_and_issue_ads'),
('countries/[0/]', 'US'),
('impression_search_field', 'has_impressions_lifetime'),
('view_all_page_id', '38471053686'),
)
data = {'__a': '1', }
with requests.session() as s:
response = s.post('https://www.facebook.com/ads/library/async/search_ads/', params=params, data=data)
ads = json.loads(response.text.replace('for (;;);', ''))['payload']['results']
for ad in ads:
ad_details_params = (
('ad_archive_id', ad[0]['adArchiveID']),
('country', 'US'),
)
response = s.post('https://www.facebook.com/ads/library/async/insights/', params=ad_details_params, data=data)
print('parse json from response')
Not: Facebook not allows for automated data collection without written permission https://www.facebook.com/apps/site_scraping_tos_terms.php
But as we all know, Facebook does not refuse to collect our data.
每个广告详细信息的响应如下:
{
"__ar": 1,
"payload": {
"ageGenderData": [
{
"age_range": "18-24",
"female": 0.03,
"male": 0.05,
"unknown": 0
},
{
"age_range": "25-34",
"female": 0.12,
"male": 0.12,
"unknown": 0.01
},
{
"age_range": "35-44",
"female": 0.16,
"male": 0.09,
"unknown": 0
},
{
"age_range": "45-54",
"female": 0.11,
"male": 0.05,
"unknown": 0
},
{
"age_range": "55-64",
"female": 0.09,
"male": 0.04,
"unknown": 0
},
{
"age_range": "65+",
"female": 0.09,
"male": 0.03,
"unknown": 0
}
],
"currency": "USD",
"currencyMatched": true,
"impressions": "35\u00a0B - 40\u00a0B",
"locationData": [
{
"reach": 0,
"region": "Alabama"
},
{
"reach": 0,
"region": "Utah"
},
{
"reach": 0,
"region": "Maine"
},
{
"reach": 0,
"region": "Louisiana"
},
{
"reach": 0,
"region": "Kentucky"
},
{
"reach": 0,
"region": "Kansas"
},
{
"reach": 0,
"region": "Idaho"
},
{
"reach": 0,
"region": "Delaware"
},
{
"reach": 0,
"region": "Connecticut"
},
{
"reach": 0,
"region": "Arkansas"
},
{
"reach": 0,
"region": "Hawaii"
},
{
"reach": 0,
"region": "Alaska"
},
{
"reach": 0,
"region": "Montana"
},
{
"reach": 0,
"region": "West Virginia"
},
{
"reach": 0,
"region": "Vermont"
},
{
"reach": 0,
"region": "Mississippi"
},
{
"reach": 0,
"region": "Wyoming"
},
{
"reach": 0,
"region": "Oklahoma"
},
{
"reach": 0,
"region": "North Dakota"
},
{
"reach": 0,
"region": "New Mexico"
},
{
"reach": 0,
"region": "New Hampshire"
},
{
"reach": 0,
"region": "Nebraska"
},
{
"reach": 0,
"region": "Rhode Island"
},
{
"reach": 0,
"region": "South Dakota"
},
{
"reach": 0.01,
"region": "Wisconsin"
},
{
"reach": 0.01,
"region": "Missouri"
},
{
"reach": 0.01,
"region": "Oregon"
},
{
"reach": 0.01,
"region": "Minnesota"
},
{
"reach": 0.01,
"region": "Maryland"
},
{
"reach": 0.01,
"region": "New Jersey"
},
{
"reach": 0.01,
"region": "Tennessee"
},
{
"reach": 0.01,
"region": "Washington, District of Columbia"
},
{
"reach": 0.01,
"region": "Indiana"
},
{
"reach": 0.02,
"region": "Michigan"
},
{
"reach": 0.02,
"region": "Iowa"
},
{
"reach": 0.02,
"region": "North Carolina"
},
{
"reach": 0.02,
"region": "Georgia"
},
{
"reach": 0.02,
"region": "Colorado"
},
{
"reach": 0.02,
"region": "Ohio"
},
{
"reach": 0.02,
"region": "Arizona"
},
{
"reach": 0.02,
"region": "Pennsylvania"
},
{
"reach": 0.02,
"region": "Virginia"
},
{
"reach": 0.03,
"region": "Washington"
},
{
"reach": 0.03,
"region": "Massachusetts"
},
{
"reach": 0.04,
"region": "Illinois"
},
{
"reach": 0.04,
"region": "Florida"
},
{
"reach": 0.06,
"region": "New York"
},
{
"reach": 0.13,
"region": "California"
},
{
"reach": 0.19,
"region": "Texas"
}
],
"singleCountry": "US",
"spend": "$500 - $599",
"pageSpend": {
"currentWeek": null,
"isPoliticalPage": true,
"weeklyByDisclaimer": {
"WARREN FOR PRESIDENT, INC.": 270970
},
"lifetimeByDisclaimer": {
"Elizabeth for MA": 781272,
"Warren for President": 3396973,
"": 13584,
"WARREN FOR PRESIDENT, INC.": 4081618,
"the Elizabeth Warren Presidential Exploratory Committee": 219471
},
"hasPoliticalSpendInAnyCountry": true
},
"pageBlurb": "United States Senator from Massachusetts, former teacher, and candidate for President of the United States. (official campaign account)"
},
"bootloadable": {},
"ixData": {},
"bxData": {},
"gkxData": {},
"qexData": {},
"lid": "6796246259692811543"
}
最后,要从 R 运行此 python 代码,请使用 reticulate
,然后将整个 python 脚本作为字符串运行 - 请注意,如果 python 脚本不包含任何 "
字符,这样可以非常方便地直接放入 R,就像这样
library(reticulate)
py_run_string("import json
import requests
rest of script etc
etc
etc")
此外,您还需要安装脚本使用的两个 python 库。这可以通过在 Mac 上打开终端,然后输入 pip install json
来安装 json
python 库,并输入 pip install requests
来完成请求库)
关于r - 通过带有嵌入式传单 svg 等的 RSelenium 提取底层数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60134698/
我已将 r 更新到最新版本,并再次安装了 Rselenium。 我尝试使用 rsDriver,但端口 4567 有问题。与 this 相同 错误回溯: rd <-rsDriver(verbose =T
我找到了@jdharrison 关于如何在 Windows 上使用 RSelenium 启动 Tor 的绝妙答案: https://stackoverflow.com/a/39048970/78373
我想使用 RSelenium 和 Firefox 浏览器从网站下载文件。 我做的一切都是正确的(导航,选择正确的元素并写下我想要的); 现在我单击“下载”按钮,然后会打开一个 Firefox 弹出窗口
我正在使用 RSelenium 构建一个包含有关经理信息的数据框。我在选择下拉列表中的元素时遇到问题。 我的代码如下: > require(RSelenium) > remDr remDr$open(
我不确定是不是因为我的网速很慢,但我正在尝试抓取一个在您向下滚动页面时加载信息的网站。我正在执行一个转到页面末尾的脚本,并等待 Selenium/Chrome 服务器加载其他内容。服务器确实更新并加载
我一整天都在使用 RSelenium,但仍然到处遇到障碍。我当前的问题是使用代码 WebElemReports$clickElement() 单击链接并打开一个新窗口。我尝试调整“about:conf
如何使用 Rselenium 抓取下拉列表中的所有选项值? 页面源代码示例: Area 1 Area 2 Area 3 Area 4 Area 4 想要的结果是一个向量,每个值都是一个元素。例如,
我一整天都在使用 RSelenium,但仍然到处遇到障碍。我当前的问题是使用代码 WebElemReports$clickElement() 单击链接并打开一个新窗口。我尝试调整“about:conf
如何使用 Rselenium 抓取下拉列表中的所有选项值? 页面源代码示例: Area 1 Area 2 Area 3 Area 4 Area 4 想要的结果是一个向量,每个值都是一个元素。例如,
我将如何并行运行RSelenium。 以下是并行使用 rvest 的示例 library(RSelenium) library(rvest) library(magrittr) library(for
我正在尝试使用 RSelenium。这是我正在做的事情: library(RSelenium) driver<- rsDriver(browser=c("chrome")) remDr <- dri
我在 RSelenium 中使用以下代码来打开浏览器。在我关闭浏览器,甚至通过运行 remDr$close() 关闭处理程序后,该端口仍在使用中。我必须转到终端并手动终止该进程,以便同一端口可用。是否
我是 RSelenium 的新手,在使用“Introduction Vignette”中的以下示例时遇到了问题: remDr$navigate("http://www.r-project.org")
我使用 RSelenium 填写网络表单。要从下拉列表中选择一个选项,我使用以下命令: xpathoption <- paste0("//select[@id = '",samplepatient[p
我尝试使用以下命令安装和运行 R Selenium 包的简单示例: install.packages("RSelenium") library("RSelenium") startServer() c
我正在尝试使用 RSelenium 抓取网站。但是,当我想连接到 Selenium 服务器时遇到问题。 假设我使用 rsDriver() 命令启动 selenium 服务器和浏览器: rsDriver
在 Linux 上,RSelenium/Selenium似乎行为不正常。我手动启动服务器,它似乎启动正常。有时我可以从我的 R session 连接到它,有时我会收到错误消息。我还不能查明原因:相同的
SO (Question 1) 中已经提出了一些与此类似的问题。 , (Question 2) ,但没有一个得到答复(最后一个有@jdharrison 的相关评论,指示 OP 提出问题)。这是我的问题
我希望使用 RSelenium 将一些基因名称输入到一个在线存储库中,该存储库为所述基因创建功能注释热图。 但是,我正在努力研究如何将基因列表输入到文本框中以生成热图。 这是文本框和与之关联的 htm
我正在尝试使用RSelenium通过按钮捕获网页上可用的csv。相关的html是: Download 我可以通过其类选择i元素: remDr$findElement(using = 'css sel
我是一名优秀的程序员,十分优秀!