【问题标题】:Web Crawler using R使用 R 的网络爬虫
【发布时间】:2018-06-07 22:28:14
【问题描述】:

我想为网站“https://www.latlong.net/convert-address-to-lat-long.html”构建一个使用R程序的网络爬虫,它可以使用地址参数访问该网站,然后从该网站获取生成的纬度和经度。这将重复我拥有的数据集的长度。

由于我是网络爬虫领域的新手,我会寻求指导。

提前致谢。

【问题讨论】:

  • 因为它没有 API,所以最好的选择是 Rselenium
  • 如果网页没有内置 API,这将是非常困难的。如果你有兴趣在没有这个思想实验的情况下对地址进行地理定位,R 中的 ggmap 包有工具。见stackoverflow.com/questions/44290940/r-geocoding-with-address/…
  • 您要进行的操作称为地理编码,根据我的经验,它通常不是免费的;充其量你可以希望每天提供有限数量的免费请求的受限 API(例如 Google)。因此,以您希望的自动方式使用本网站可能违反服务条款。
  • 虽然该网站允许网页抓取(请参阅 siteaddress/robots.txt),但它在后台调用maps.googleapis.com/maps/js/GeocodeService.Search,这不是免费的

标签: r web-scraping rcrawler


【解决方案1】:

过去我使用过一个名为 IP 堆栈 (ipstack.com) 的 API。

示例:数据框“d”包含名为“ipAddress”的 IP 地址列

for(i in 1:nrow(d)){
  #get data from API and save the text to variable 'str'
  lookupPath <- paste("http://api.ipstack.com/", d$ipAddress[i], "?access_key=INSERT YOUR API KEY HERE&format=1", sep = "")
  str <- readLines(lookupPath)

  #save all the data to a file
  f <- file(paste(i, ".txt", sep = ""))
  writeLines(str,f)
  close(f)

  #save data to main data frame 'd' as well:
  d$ipCountry[i]<-str[7]
  print(paste("Successfully saved ip #:", i))
}

在这个例子中,我专门在每个 IP 的 Country 位置之后,它出现在 API 返回的数据的第 7 行(因此是 str[7])

此 API 可让您每月免费查找 10,000 个地址,这对我的目的来说已经足够了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-11
    相关资源
    最近更新 更多