gpt4 book ai didi

r - 填写网页表格,提交和下载结果

转载 作者:行者123 更新时间:2023-12-01 01:40:33 25 4
gpt4 key购买 nike

我想填一个 web form并提交我的查询并下载结果数据。某些字段可以选择下拉菜单或输入搜索查询,部分也可以留空(如果所有部分都留空,则整个数据库都会下载),点击“搜索和下载”按钮应该会启动下载文件。

这是我基于 this 尝试过的(选择物种“Salmo salar”的所有记录)题。我使用我的浏览器(Opera)“开发者工具”来检查页面元素并识别所有可能字段的名称:

library(httr)

url <- "https://nzffdms.niwa.co.nz/search"

fd <- list(
search_catchment_no_name = "",
search_river_lake = "",
search_sampling_locality = "",
search_fishing_method = "",
search_start_year = "",
search_end_year = "",
search_species = "Salmo salar", # species of interest
search_download_format = 1, # select csv file format
submit = "Search and Download"
)

POST(url, body = fd, encode = "form")

我曾希望这会导致下载一个 csv 文件(物种“Salmo salar”的所有记录),但没有文件下载(但输出这个(10 个列表,只显示第一位):
Response [https://nzffdms.niwa.co.nz/search]
Date: 2019-10-02 23:35
Status: 200
Content-Type: text/html; charset=utf-8
Size: 19.1 kB
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; c...
<meta name="title" content="NZ Freshwater Fish Database...
<meta name="description" content="NIWA NZ Freshwater Fish...
<meta name="keywords" content="NIWA, NZ, Freshwater Fish" />
<meta name="language" content="en" />
<meta name="robots" content="index, follow />

...

编辑

我认为问题在于我如何调用 Search and download按钮,在检查网页时,大多数字段如下所示:
# end year field
<input maxlength="4" class="form-control" type="text" name="search[end_year]" id="search_end_year">

但是 search and download按钮元素没有 nameid选项:
<input type="submit" value="Search and Download" class="btn btn-primary btn-md">

另外我刚刚注意到有一个隐藏字段,也许我需要定义这个?
<input type="hidden" name="search[_csrf_token]" value="d1530f09c1ce8110b5163bd100cb0d67" id="search__csrf_token">

关于如何下载文件的任何建议将不胜感激。

最佳答案

首先,检查 website 上的 robots.txt .已于 2019 年 10 月 3 日注释掉。

然后阅读 https://nzffdms.niwa.co.nz/terms 上的条款和条件和 https://www.niwa.co.nz/freshwater-and-estuaries/nzffd/user-guide/tips并确保您遵守条款和条件。

限制下面的请求也很重要。

检查所有条款和条件后,您可以使用以下代码查询您的数据:

library(httr)
library(xml2)

gr <- GET("https://nzffdms.niwa.co.nz/search")
doc <- read_html(content(gr, "text")) #doc <- read_html(gr) #this works as well
getTbl <- function(x) {
do.call(rbind, lapply(xml_find_all(doc, paste0(".//select[@name='search",x,"']/option")),
function(n) data.frame(NAME=xml_text(n), VALUE=xml_attr(n, "value"))))
}
fishing_method <- getTbl("[fishing_method]")
species <- getTbl("[species][]")
csrf_token <- xml_attr(xml_find_all(doc, ".//input[@name='search[_csrf_token]']"), "value")

fd <- list(
"search[catchment_no_name]"="",
"search[river_lake]"="",
"search[sampling_locality]"="",
"search[fishing_method]"="",
"search[species][]"="",
"search[species][]"=68,
"search[start_year]"="",
"search[end_year]"="",
"search[download_format]"="1",
"search[_csrf_token]"=csrf_token
)
r <- POST("https://nzffdms.niwa.co.nz/doSearch", body=fd, encode="form")
read.csv(text=content(r, "text", encoding="UTF-8"))

输出:
   card m    y catchname  catch        locality time  org map    east   north altitude penet fishmeth effort pass spcode abund number minl maxl  nzreach
1 3964 1 1981 Waiau R 797.49 Lake Gunn NA niwa d41 2122400 5581200 477 225 ang NA NA salsal NA NA NA NA 15006671
2 3965 1 1981 Waiau R 797.49 Lake Fergus NA niwa d41 2123700 5584400 483 229 ang NA NA salsal NA NA NA NA 15006092
3 15975 1 2003 Waiau R 797.40 Excelsior Creek 1330 niwa d44 2095800 5495800 190 94 efp 80 1 salsal NA 2 102 105 15030686
4 50772 1 1940 Waiau R 797.49 Upukerora River NA unk d43 2098500 5519900 210 146 unk NA NA salsal NA NA NA NA 15020897

关于r - 填写网页表格,提交和下载结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58159645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com