使用Colly框架无法登录Evernote账户,请求技术指导
Fixing Evernote Login Issues with Colly
Hey there! The approach you're using—directly POSTing username and password to the login endpoint—isn't going to work for Evernote, and here's why, plus how to fix it:
Why Your Current Code Fails
- Missing Required Form Parameters: Modern login systems (like Evernote's) don't just accept a username and password. They typically require additional hidden fields (like a CSRF token) that the server generates when you load the login page. Without these, the server will reject your login request as unauthenticated.
- Incorrect Request Flow: You're sending the POST request before even loading the login page. The server needs to set initial cookies and generate necessary tokens for your session first, so skipping this step breaks the authentication flow.
The Correct Approach
You need to first visit the login page to capture all required form fields and session cookies, then submit the complete form data including those fields. Here's how to adjust your code:
package main import ( "log" "strings" "github.com/gocolly/colly" "github.com/gocolly/colly/extensions" ) func main() { // Create a collector with session persistence c := colly.NewCollector( colly.AllowURLRevisit(), ) extensions.RandomUserAgent(c) // Mimic a real browser's user agent to avoid bot detection // Store form values we need to submit formData := make(map[string]string) // First, visit the login page to capture hidden fields and cookies c.OnHTML("form#loginForm", func(e *colly.HTMLElement) { // Extract all input fields from the login form e.ForEach("input", func(_ int, el *colly.HTMLElement) { name := el.Attr("name") value := el.Attr("value") if name != "" { formData[name] = value } }) // Override with your actual credentials formData["username"] = "XXXXXX@XXX.com" formData["password"] = "*********" // Submit the login form with all captured fields err := e.Request.Submit(formData) if err != nil { log.Fatal("Failed to submit login form:", err) } }) // Handle responses to check login status c.OnResponse(func(r *colly.Response) { log.Println("Response Status:", r.StatusCode) // Verify login success by checking for content that appears after authentication if strings.Contains(string(r.Body), "My Notes") || r.Request.URL.Path == "/" { log.Println("Login successful!") // Now you can visit authenticated pages c.Visit("https://www.evernote.com/") } }) // Log any request errors c.OnError(func(_ *colly.Response, err error) { log.Println("Request error:", err) }) // Start the flow by loading the login page err := c.Visit("https://www.evernote.com/Login.action") if err != nil { log.Fatal("Failed to load login page:", err) } }
Key Changes Explained
- Visit Login Page First: This lets us capture hidden form fields (like CSRF tokens) that Evernote expects in the login request—without these, the server will block your attempt.
- Extract All Form Fields: Using
OnHTMLto parse the login form ensures we don't miss any required parameters the server validates. - Session Persistence: Colly automatically handles cookies, so once we submit the valid form, subsequent
Visitcalls will use your authenticated session. - User Agent Spoofing: Adding a random user agent helps avoid being flagged as a bot by Evernote's anti-scraping measures.
Important Notes
- Evernote may have additional anti-bot safeguards (like CAPTCHAs) that could block automated login attempts. If you hit this wall, using Evernote's official API is the recommended, reliable way to access your data programmatically.
- Always double-check form field names by inspecting the login page's HTML (right-click → Inspect) to ensure you're using the correct keys in
formData.
内容的提问来源于stack exchange,提问作者John Davis
相关产品推荐
相关产品推荐

