网页抓取循环

huangapple go评论101阅读模式
英文:

web scraping with loop

问题

我试图下载不同页面上的邮政编码。我从墨西哥城内每个自治市的节点列表开始。

  1. url <- "https://www.codigopostal.lat/mexico/Ciudad-de-Mexico/"
  2. resource <- GET(url)
  3. parse <- htmlParse(resource)
  4. links <- as.character(xpathSApply(parse, path = "//a", xmlGetAttr, "href"))
  5. print(links)

然后,我尝试创建一个循环,获取每个 URL 并提取邮政编码表格,以后创建每个自治市创建的矩阵的大语料库。

  1. scraper <- function(url){
  2. html <- read_html(url)
  3. tabla <- html %>%
  4. html_elements("td , th") %>%
  5. html_text2()
  6. data <- matrix(ncol = 3, nrow = length(tabla))
  7. data <- data.frame(matrix(tabla, nrow = length(tabla), ncol = 3, byrow = TRUE)) %>%
  8. row_to_names(row_number = 1)
  9. }

我将拥有 "municipality"、"locality" 和 "zp",因此列数为 3,但似乎出现了错误:"Error: x must be a string of length 1",而且我也无法合并所有矩阵。有任何想法都会非常感激!

英文:

I'm trying to download zip codes that are in different pages. I started with a list of nodes for each municipality inside Mexico City.

  1. url&lt;-&quot;https://www.codigopostal.lat/mexico/Ciudad-de-Mexico/&quot;
  2. resource&lt;-GET(url)
  3. parse&lt;-htmlParse(resource)
  4. links&lt;-as.character(xpathSApply(parse,path=&quot;//a&quot;,xmlGetAttr,&quot;href&quot;))
  5. print(links)

And then I'm trying to create a loop that grabs each url and grabs the table of zip codes to later create a big corpus of each matrix created per municipality:

  1. scraper&lt;-function(url){
  2. html&lt;-read_html(url)
  3. tabla&lt;-html%&gt;%
  4. html_elements(&quot;td , th&quot;) %&gt;%
  5. html_text2()
  6. data&lt;-matrix(ncol=3,nrow=length(tabla))
  7. data&lt;-data.frame(matrix(tabla,nrow=length(tabla),ncol=3,byrow=TRUE)) %&gt;%
  8. row_to_names(row_number=1)
  9. }

I will have "municipality", "locality", "zp", that's why the number of columns is 3, but it seems that:
"Error: x must be a string of length 1" and I also cannot add up all the matrices.
Any ideas are greatly appreciated!

答案1

得分: 1

这是一种获取墨西哥城邮政编码的方法。

  1. suppressPackageStartupMessages({
  2. library(rvest)
  3. library(magrittr)
  4. })
  5. scraper <- function(link) {
  6. link %>%
  7. read_html() %>%
  8. html_table() %>%
  9. `[[`(1)
  10. }
  11. url <- "https://www.codigopostal.lat/mexico/Ciudad-de-Mexico/"
  12. page <- read_html(url)
  13. zip_codes_list <- page %>%
  14. html_elements("a") %>%
  15. html_attr("href") %>%
  16. grep("mexico/Ciudad-de-Mexico/.+", ., value = TRUE) %>%
  17. lapply(scraper)

然后将它们全部合并在一起。

  1. zip_codes <- do.call(rbind, zip_codes_list)

编辑

在原始帖子中,我加载了dplyr包。经过再次考虑,我意识到它只是加载了相关包magrittr,以使magrittr管道运算符可用,因此我已更改代码只加载相关包magrittr

英文:

Here is a way to scrape the zip codes of Ciudad-de-Mexico.

  1. suppressPackageStartupMessages({
  2. library(rvest)
  3. library(magrittr)
  4. })
  5. scraper &lt;- function(link) {
  6. link %&gt;%
  7. read_html() %&gt;%
  8. html_table() %&gt;%
  9. `[[`(1)
  10. }
  11. url &lt;- &quot;https://www.codigopostal.lat/mexico/Ciudad-de-Mexico/&quot;
  12. page &lt;- read_html(url)
  13. zip_codes_list &lt;- page %&gt;%
  14. html_elements(&quot;a&quot;) %&gt;%
  15. html_attr(&quot;href&quot;) %&gt;%
  16. grep(&quot;mexico/Ciudad-de-Mexico/.+&quot;, ., value = TRUE) %&gt;%
  17. lapply(scraper)

Then rbind them all together.

  1. zip_codes &lt;- do.call(rbind, zip_codes_list)

Edit

In the original post I have loaded package dplyr. After a second thought I have realized that it's only loaded to make the magrittr pipe operator available, so I have changed the code to only load the relevant package, magrittr.

答案2

得分: 1

  1. library(tidyverse)
  2. library(rvest)
  3. "https://www.codigopostal.lat/mexico/Ciudad-de-Mexico/" %>%
  4. read_html() %>%
  5. html_elements(".ctrLink a") %>%
  6. html_attr("href") %>% # 从主URL中获取所有链接
  7. map_dfr(~ .x %>% # 遍历市政府,抓取表格并合并行
  8. read_html() %>%
  9. html_table())
  10. # 最后考虑 janitor::clean_names()
  11. # 一个数据框:2,014 行 x 3 列
  12. # 市政府 地区 邮政编码
  13. # <chr> <chr> <int>
  14. # 1 阿尔瓦罗·奥布雷贡 第1扩建总统 1299
  15. # 2 阿尔瓦罗·奥布雷贡 第1部分Cañada 1269
  16. # 3 阿尔瓦罗·奥布雷贡 第1胜利 1160
  17. # 4 阿尔瓦罗·奥布雷贡 第2扩建总统 1299
  18. # 5 阿尔瓦罗·奥布雷贡 第2 Del Moral del Pueblo de Tetelpan 1700
  19. # 6 阿尔瓦罗·奥布雷贡 第2部分Cañada 1269
  20. # 7 阿尔瓦罗·奥布雷贡 第2 Reacomodo Tlacuitlapa 1650
  21. # 8 阿尔瓦罗·奥布雷贡 8月8日 1180
  22. # 9 阿尔瓦罗·奥布雷贡 云杉 1440
  23. # 10 阿尔瓦罗·奥布雷贡 亚伯拉罕·M.冈萨雷斯 1170
  24. # … 其他 2,004 行
  25. # 通过 `print(n = ...)` 查看更多行
英文:
  1. library(tidyverse)
  2. library(rvest)
  3. &quot;https://www.codigopostal.lat/mexico/Ciudad-de-Mexico/&quot; %&gt;%
  4. read_html() %&gt;%
  5. html_elements(&quot;.ctrLink a&quot;) %&gt;%
  6. html_attr(&quot;href&quot;) %&gt;% # Grab all links from the main URL
  7. map_dfr(~ .x %&gt;% # Map through municipalities, scrape tables and row bind
  8. read_html() %&gt;%
  9. html_table())
  10. # Consider janitor::clean_names() at the end
  11. # A tibble: 2,014 &#215; 3
  12. Municipio Localidad `C&#243;digo Postal`
  13. &lt;chr&gt; &lt;chr&gt; &lt;int&gt;
  14. 1 Alvaro Obregon 1a Ampliaci&#243;n Presidentes 1299
  15. 2 Alvaro Obregon 1a Secci&#243;n Ca&#241;ada 1269
  16. 3 Alvaro Obregon 1a Victoria 1160
  17. 4 Alvaro Obregon 2a Ampliaci&#243;n Presidentes 1299
  18. 5 Alvaro Obregon 2a Del Moral del Pueblo de Tetelpan 1700
  19. 6 Alvaro Obregon 2a Secci&#243;n Ca&#241;ada 1269
  20. 7 Alvaro Obregon 2o Reacomodo Tlacuitlapa 1650
  21. 8 Alvaro Obregon 8 de Agosto 1180
  22. 9 Alvaro Obregon Abeto 1440
  23. 10 Alvaro Obregon Abraham M. Gonz&#225;lez 1170
  24. # … with 2,004 more rows
  25. # ℹ Use `print(n = ...)` to see more rows

huangapple
  • 本文由 发表于 2023年2月27日 10:14:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75576268.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定