【问题标题】:Java crawler to get all first and third party cookies获取所有第一方和第三方 cookie 的 Java 爬虫
【发布时间】:2019-05-13 16:57:09
【问题描述】:

我想用 Java 构建一个爬虫,它可以为我提供来自网站的所有 cookie。这个爬虫被认为是自动爬取网站列表(显然是底部)。

我的计划中使用了 jSoup 和 Selenium。

package com.mycompany.app;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;

public class BasicWebCrawler {
    private static Set<String> uniqueURL = new HashSet<String>();
    private static List<String> link_list = new ArrayList<String>();

    private static Set<String> uniqueCookies = new HashSet<String>();

    private static void get_links(String url) {
        Connection connection = null;
        Connection.Response response = null;
        String this_link = null;

        try {
            connection = Jsoup.connect(url);
            response = connection.execute();

            //cookies_http = response.cookies();

            // fetch the document over HTTP
            Document doc = response.parse();

            // get all links in page
            Elements links = doc.select("a[href]");

            if(links.isEmpty()) {
                return;
            }

            for (Element link : links) {
                this_link = link.attr("href");

                boolean add = uniqueURL.add(this_link);

                System.out.println("\n" + this_link + "\n" + "title: " + doc.title());

                if (add && (this_link.contains(url))) {
                    System.out.println("\n" + this_link + "\n" + "title: " + doc.title());

                    link_list.add(this_link);

                    get_links(this_link);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        get_links("https://de.wikipedia.org/wiki/Wikipedia");

        /**
         * Hier kommt Selenium ins Spiel
         */
        WebDriver driver;

        System.setProperty("webdriver.chrome.driver", "D:\\crawler\\driver\\chromedriver.exe");

        driver = new ChromeDriver();

        // create file named Cookies to store Login Information
        File file = new File("Cookies.data");

        FileWriter fileWrite = null;
        BufferedWriter Bwrite = null;

        try {
            // Delete old file if exists
            file.delete();
            file.createNewFile();

            fileWrite = new FileWriter(file);
            Bwrite = new BufferedWriter(fileWrite);
            // loop for getting the cookie information
        } catch (Exception ex) {
            ex.printStackTrace();
        }


        for(String link : link_list) {
            System.out.println("Open Link: " + link);

            driver.get(link);

            try {
                // loop for getting the cookie information
                for (Cookie ck : driver.manage().getCookies()) {
                    String tmp = (ck.getName() + ";" + ck.getValue() + ";" + ck.getDomain() + ";" + ck.getPath() + ";" + ck.getExpiry() + ";" + ck.isSecure());

                    if(uniqueCookies.add(tmp)) {
                        Bwrite.write("Link: " + link + "\n" + (ck.getName() + ";" + ck.getValue() + ";" + ck.getDomain() + ";" + ck.getPath() + ";" + ck.getExpiry() + ";" + ck.isSecure())+ "\n\n");
                        Bwrite.newLine();
                    }
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        try {
            Bwrite.close();
            fileWrite.close();

            driver.close();
            driver.quit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

我在wikipedia 页面上测试此代码,并将结果与​​cookie 扫描程序调用CookieMetrix 进行比较。

我的代码只显示了四个 cookie:

Link: https://de.wikipedia.org/wiki/Wikipedia:Lizenzbestimmungen_Commons_Attribution-ShareAlike_3.0_Unported
GeoIP;DE:NW:M__nster:51.95:7.54:v4;.wikipedia.org;/;null;true


Link: https://de.wikipedia.org/wiki/Wikipedia:Lizenzbestimmungen_Commons_Attribution-ShareAlike_3.0_Unported
WMF-Last-Access-Global;13-May-2019;.wikipedia.org;/;Mon Jan 19 02:28:33 CET 1970;true


Link: https://de.wikipedia.org/wiki/Wikipedia:Lizenzbestimmungen_Commons_Attribution-ShareAlike_3.0_Unported
WMF-Last-Access;13-May-2019;de.wikipedia.org;/;Mon Jan 19 02:28:33 CET 1970;true


Link: https://de.wikipedia.org/wiki/Wikipedia:Lizenzbestimmungen_Commons_Attribution-ShareAlike_3.0_Unported
mwPhp7Seed;55e;de.wikipedia.org;/;Mon Jan 19 03:09:08 CET 1970;false

但是 cookie 扫描器显示 7 个。我不知道为什么我的代码显示比 CookieMetrix 少。你能帮帮我吗?

【问题讨论】:

  • 您的代码相当“杂乱”——您应该将其归结为基本要素。最重要的是:您使用的是 Selenium 还是 jsoup?
  • 我认为这是必不可少的代码,因为代码越少,没人理解代码的作用。在代码中,您可以看到我使用了 jsoup 和 selenium。我使用 jsoup 来抓取所有底面的链接,并使用 selenium 来连接所有这些面以获取(javascript-)cookie。
  • 你的爬虫没有得到哪 3 个 cookie?
  • 我没有分析你的代码也没有分析维基百科站点,但是你的爬虫没有找到所有链接,因为它们可能是由 JavaScript 生成的吗?您是否也尝试过仅使用硒的方法?如果我的想法是正确的,那么使用完整的 Selenium 爬虫,你可能会得到所有的 cookie。
  • 你试过puppeteer或者cookieserve这样的服务

标签: java selenium web-scraping web-crawler jsoup


【解决方案1】:

java.util.Set&lt;Cookie&gt; getCookies() 的 JavaDoc:

获取当前域的所有cookie。这相当于调用“document.cookie”并解析结果

  1. document.cookie不会返回 HttpOnly cookie,因为 JavaScript 不允许这样做。

  2. 另请注意,“CookieMetrix”似乎列出了来自不同域的 cookie。

解决方案:

  • 要获得诸如“CookieMetrix”(1+2) 之类的列表,您可以在浏览器之后添加代理并嗅探请求。

  • 如果您想获取当前域的所有 cookie,包括 HttpOnly (1),您可以尝试直接访问 Chrome 的 DevTools API(当然,它也会返回 HttpOnly cookie)

【讨论】:

  • 所以我必须用 selenium 抓取网站以获取所有由 javascript 生成的链接,然后我可以打开与 jSoup 的连接以获取 HTTP 响应。我可以在响应中搜索“Set-cookie”以获取 HttpOnly cookie 吗?
  • 我不会将 Jsoup 添加到聚会中。正如我所说,我的解决方案是将 Selenium 浏览器放在代理后面并嗅探标题。
  • 我尝试只用硒制作一个爬虫,但我会有一个 StaleElementReferenceException。你能帮我this new thread吗?
猜你喜欢
  • 1970-01-01
  • 2011-03-22
  • 2020-10-13
  • 2014-11-09
  • 1970-01-01
  • 2010-10-11
  • 2013-12-08
  • 2012-06-28
  • 2019-05-28
相关资源
最近更新 更多