You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R语言tidyverse中调用session_submit登录网站报错:'all components of query must be named'问题求助

Troubleshooting "Error: All components of query must be named" in rvest Login

Hey there, let's break down what might be causing this error and how to fix it. That error message usually pops up when a URL query string has an unnamed parameter, which doesn't make sense for web requests—every parameter needs a key-value pair. Since your script worked last year but broke now, here are the most likely culprits and fixes:

1. Website Form Structure Changed (Most Probable)

Websites often update their login forms to add security measures like CSRF tokens, session IDs, or other hidden fields that weren't there before. These fields are required for a successful login, but if your code isn't handling them, it can lead to malformed requests that trigger this error.

First, inspect the full structure of the login form to check for hidden fields you might be missing:

# Print the complete form details to see all fields (including hidden ones)
str(loginForm)

# Or list all field names explicitly
names(loginForm$fields)

Look for fields like authenticity_token, csrf_token, or session_key—these are common hidden fields. If you find them, you'll need to include their values when filling the form. For example:

# Extract the hidden token value from the form
csrf_token <- loginForm$fields$authenticity_token$value

# Fill the form with ALL required fields (username, password, and the token)
loginFormFilled <- set_values(loginForm,
  username = "myname",
  password = "mypassword",
  authenticity_token = csrf_token
)

2. Inspect the Actual Request Being Sent

To see exactly what's causing the malformed query, enable verbose logging for the request. This will show you the full URL, parameters, and headers being sent, which can uncover unnamed parameters:

# Add verbose config to see the raw request details
session_submit(x = session, form = loginFormFilled, config = verbose())

Look for any parts of the URL or request body that have a value without a corresponding key (like ?undefined=123 or a blank parameter name).

3. Check for Package Version Changes

Between last year and now, rvest or its dependency httr might have updated, changing how form submissions are handled. Older versions might have been more lenient with unnamed parameters, while newer versions enforce stricter rules.

Check your package versions:

packageVersion("rvest")
packageVersion("httr")

If you suspect a version issue, try rolling back to the versions that worked last year (use devtools::install_version() to install specific versions) and test again.

4. Manually Build the POST Request (As a Workaround)

If session_submit is still giving you trouble, bypass it and use httr::POST directly to have full control over the request:

# Get the form's action URL (where the data is sent)
action_url <- loginForm$action

# Create a data frame of all form fields (including hidden ones)
form_fields <- map_dfr(loginForm$fields, ~tibble(name = .x$name, value = .x$value))

# Replace username and password with your credentials
form_fields <- form_fields %>%
  mutate(value = case_when(
    name == "username" ~ "myname",
    name == "password" ~ "mypassword",
    TRUE ~ value
  ))

# Convert to a named list for the POST request
form_data <- deframe(form_fields)

# Send the POST request and update the session
response <- POST(session$url %/% action_url, body = form_data, set_cookies(session$cookies))
session <- session_jump_to(session, response)

This method ensures you're sending exactly the parameters the form expects, no surprises.


内容的提问来源于stack exchange,提问作者Sam Asin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 13:52:31