RCurl用于谷歌Adwords中

时间:2022-08-06 06:18:58

I'd like to download data pertaining to keywords straight into/from R. I understand that RCurl would most probably be the way to go, but I am not sure how to proceed with the task. Perhaps anybody here could put me on the right track?

我想直接从r下载与关键字相关的数据。我知道RCurl可能是最合适的方法,但是我不确定该如何继续这个任务。也许这里有人能让我走上正轨吗?

PS. I may slightly edit this question as answers pour in because I have some ideas of how I could possibly download Adwords data using R; however, the ideas are unclear, and any answers would probably make them clearer.

我可能会稍微编辑一下这个问题,因为我有一些关于如何使用R下载Adwords数据的想法;然而,这些想法并不清晰,任何答案都可能使它们更加清晰。

Many thanks.

多谢。


EDIT: My attempt

编辑:我尝试

The following is what I tried until now.

下面是我一直在尝试的。

1. apply getURL on the login URL to identify the ids of the Email and Password fields

1。在登录URL上应用getURL来标识电子邮件和密码字段的id

require(RCurl)
loginURL<- "https://accounts.google.com/ServiceLogin?service=adwords"
ch<- getCurlHandle()
curlSetOpt(curl=ch,ssl.verifypeer=FALSE,cainfo=system.file("CurlSSL", "cacert.pem", package = "RCurl"),cookiejar="./cookies.txt",cookiefile="./cookies.txt",verbose=TRUE,header=TRUE,followlocation=TRUE,autoreferer=TRUE)

try1<- getURL(loginURL,curl=ch)

2. I identified what the ids for the important fields (email & password) were

2。我确定了重要字段(电子邮件和密码)的id

<div class="email-div">
  <label for="Email"><strong class="email-label">Email</strong></label>
  <input type="email" spellcheck="false"  

  name="Email" id="Email" value=""

    >
</div>
<div class="passwd-div">
  <label for="Passwd"><strong class="passwd-label">Password</strong></label>
  <input type="password" name="Passwd" id="Passwd"

3. I then used the above fields to apply the postForm function at the loginURL in order to log into Google Adwords

3所示。然后,我使用上面的字段在loginURL上应用postForm函数,以便登录到谷歌Adwords

params<- list(
"Email"="myemail",
"Passwd"="mypassword",
"GALX"="3b6rR7Jvk30")

loggedIn<- postForm(loginURL,.params=params,curl=ch)

However, I have no idea how to verify that I have successfully logged in.

然而,我不知道如何验证我已经成功登录。

Plus, the URL for Kyeword planner tool in Google Adwords UI is:

另外,谷歌Adwords UI中Kyeword planner工具的URL为:

https://adwords.google.com/ko/KeywordPlanner/Home?__c=XXXXXXXXXX&__u=XXXXXXXXXX&__o=cues

where the c= reflects the customer id and the u= reflects the user id. What I thought about doing, given this, was to log in using my browser, paste the URL shown above into R, and then try to find out the ids for fields that I will be of relevance to me, such as the id for the keywords text box in the Keyword planner tool, to which I could possibly send keywords from R.

c =反映了客户id和u =反映了用户id。我想做什么,在这种情况下,使用浏览器登录,上面所示的URL粘贴到R,然后试着找出的id字段,我将相关的,比如id关键字关键字文本框的计划工具,我能发送关键字从R。

But when I try to apply getURL on the aforesaid website, I do not get the required/expected xml tags or key value pairs. Instead:

但是当我尝试在上述网站上应用getURL时,我没有得到所需的/预期的xml标记或键值对。而不是:

<html><head><noscript><meta http-equiv="refresh" content="0; URL=https://adwords.google.com/select/interstitial_short_js.html"></noscript></head><body><script type="text/javascript" language="javascript">var jsRedirect = true;var url = "/um/StartNewLogin?dst=/ko/KeywordPlanner/Home?__c%3D7857647860%26__u%3D4575929980%26__o%3Dcues";
if (self.document.location.hash) {url = url + ((url.indexOf('?') == -1)? '?' : '&') + "frag=" + self.document.location.hash.substring(1); }
window.location.assign(url);
</script> </body> </html>

This leads me to think that I am probably dealing with Javascript or AJAX here. So, how to extract data from Javascript or AJAX using RCurl, and is this the correct question to ask?

这使我认为我可能在这里处理Javascript或AJAX。那么,如何使用RCurl从Javascript或AJAX中提取数据呢?

Thanks & apologies for the lengthy edit.

感谢和歉意,为冗长的编辑。

2 个解决方案

#1


2  

Have you seen our RAdwords package in the meantime?

你看了我们的RAdwords包了吗?

It provides an authentication process for R with the Adwords API and an interface to load data from the Adwords API directly into R.

它为R提供了一个带有Adwords API的身份验证过程,以及一个接口,可以将Adwords API中的数据直接加载到R中。

Example code to load keyword data:

加载关键字数据的示例代码:

#install package from CRAN
install.packages('RAdwords')
#load package
library(RAdwords)
#start authentication process
google_auth <- doAuth()
#build statement object
body <- statement(select=c('AccountDescriptiveName','Date', 'CampaignName', 'AdGroupName','KeywordText', 'KeywordMatchType', 'Clicks', 'Cost'),
                  report="KEYWORDS_PERFORMANCE_REPORT",
                  start="20140320",
                  end="20140321")
#download data as data frame
data <- getData(clientCustomerId='xxx-xxx-xxxx',
                google_auth = google_auth,
                statement=body,
                transformation = T)
#all available report types
reports()
#all available metrics of specific report type
metrics("KEYWORDS_PERFORMANCE_REPORT")

#2


2  

I don't have enough reputation to comment (ridiculous!), hence this answer.

我没有足够的声誉去评论(荒谬!)

My answer pertains to your question's part on how to verify the result of logging in. I would do it this way:

我的回答与您关于如何验证登录结果的问题有关。我会这样做:

loggedIn<- postForm(loginURL, .params=params, curl=ch)

info <- getCurlInfo(ch)

# 1) check the value of info$response.code here and act accordingly
# 2) you probably need the value to be 200 or between 200 and 300, but for more details
# on HTTP status codes, see this page: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

#1


2  

Have you seen our RAdwords package in the meantime?

你看了我们的RAdwords包了吗?

It provides an authentication process for R with the Adwords API and an interface to load data from the Adwords API directly into R.

它为R提供了一个带有Adwords API的身份验证过程,以及一个接口,可以将Adwords API中的数据直接加载到R中。

Example code to load keyword data:

加载关键字数据的示例代码:

#install package from CRAN
install.packages('RAdwords')
#load package
library(RAdwords)
#start authentication process
google_auth <- doAuth()
#build statement object
body <- statement(select=c('AccountDescriptiveName','Date', 'CampaignName', 'AdGroupName','KeywordText', 'KeywordMatchType', 'Clicks', 'Cost'),
                  report="KEYWORDS_PERFORMANCE_REPORT",
                  start="20140320",
                  end="20140321")
#download data as data frame
data <- getData(clientCustomerId='xxx-xxx-xxxx',
                google_auth = google_auth,
                statement=body,
                transformation = T)
#all available report types
reports()
#all available metrics of specific report type
metrics("KEYWORDS_PERFORMANCE_REPORT")

#2


2  

I don't have enough reputation to comment (ridiculous!), hence this answer.

我没有足够的声誉去评论(荒谬!)

My answer pertains to your question's part on how to verify the result of logging in. I would do it this way:

我的回答与您关于如何验证登录结果的问题有关。我会这样做:

loggedIn<- postForm(loginURL, .params=params, curl=ch)

info <- getCurlInfo(ch)

# 1) check the value of info$response.code here and act accordingly
# 2) you probably need the value to be 200 or between 200 and 300, but for more details
# on HTTP status codes, see this page: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html