使用 httr 修改 html/javascript 代码

Modifying html/javascript code with httr

我正在尝试制作一个脚本,使用 alumni directory 检查是否有人去过这所学校。 (可以用弗朗索瓦·奥朗德(法国现任总统)这个名字看看表现如何)

据我所知(对于 "name" ("nom") 按钮)我需要访问 HTML 代码的这一部分:

<div class="annuaireRecherche-v2" style="">
  <span>Nom</span>
  <div class="select2-container select2-allowclear autocomplete" id="s2id_PersonneNom" style="min-width: 0;">
    <a href="javascript:void(0)" class="select2-choice" tabindex="-1">   
      <span class="select2-chosen" id="select2-chosen-3">Derez</span>
      <abbr class="select2-search-choice-close"></abbr>   
      <span class="select2-arrow" role="presentation">
        <b role="presentation"></b>
      </span>
    </a>
    <label for="s2id_autogen3" class="select2-offscreen"></label>
    <input class="select2-focusser select2-offscreen" type="text" aria-haspopup="true" role="button" aria-labelledby="select2-chosen-3" id="s2id_autogen3" tabindex="0">
  </div>
  <input type="hidden" name="PersonneNom" id="PersonneNom" class="autocomplete" style="min-width: 0px; display: none;" data-placeholder="Saisir un nom" data-multiple="" data-libelle="" value="Hollande" data-limit="" tabindex="-1" title="">
</div>

并在最后一个 <input> 标记中,将 value 属性更改为我要检查的名称。

那我就得"click"莫名其妙就afficher les résultats(翻译:show results)就对了。相关HTML代码:

<div class="showResultsButton" style="text-align: center; display: block;">
  <a href="#" class="jqueryButton  ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-primary" onclick="showResultList($('.shortResults')); return false;" role="button">
    <span class="ui-button-icon-primary ui-icon ui-icon-search"></span>
    <span class="ui-button-text">
      Afficher les résultats
    </span>
  </a>
</div>

然后我必须找到 <div class="people clearfix"> 标签并检索 <a href="..."> 标签:

<div class="people clearfix">
    <div class="tab_result" style="clear:both">
        <div class="ppl">       
            <div class="ppl-wrap clearfix" style="clear:both">
                <div class="ppl-image">
                    <a href="/profil/francois.hollande74" target="_blank">
                        <img alt="" src="/ressources/temp/100_120t121_153006959_inconnu.jpeg">
                    </a>
                </div>
                <div class="ppl-content">
                    <h3>
                        <a href="/profil/francois.hollande74" target="_blank">Hollande  François</a>
                    </h3>
                    <p class="meta">D Service Public Promo 1974</p>
                    <p></p>
                </div>
                    <div class="ppl-content" style="float:right"></div>
                </div>
            <p class="buttons">
                <a class="button " href="/profil/francois.hollande74" target="_blank">
                    Voir le profil
                </a>
            </p>
        </div>
    </div>
</div>

到目前为止,这是我的代码:

library(XML)
library(httr)
library(foreach)

url        <- "http://www.sciences-po.asso.fr/gene/main.php?base=1244"    
response   <- GET(url)
doc        <- content(response, type="text/html", encoding = 'ISO-8859-1')
parseddoc  <- htmlParse(doc)

# i have to modify the content of this 
xpathApply(parseddoc, "//*[@id='PersonneNom']/@value")
# then make sure it is sent to the server, retrieve the code sent back, etcaetera...

感谢您提供的任何帮助。

为了防止有人偶然发现这个问题,我发现了另外两个用于抓取网站的软件包:rvestRSelenium。我选择了 RSelenium,因为它似乎是最直接的:它会打开你的浏览器,你可以在网页上实时看到你的代码在做什么。

此外,这里有两个我发现非常有用的链接,第二个是 RSelenium 的一个很好的介绍:

http://ikkyle.com/webscraping_with_r.html

https://www.datacamp.com/community/tutorials/scraping-javascript-generated-data-with-r