仅抓取 HTML 页面,同时检查响应标头

发布于 2024-11-19 07:37:43 字数 2587 浏览 1 评论 0原文

我试图获取所有标题为 Content-Type:text/html 的 url,因此我检查每个 url 的响应标头,如果它们具有 content-type: text/html,那么我想打印该 url内容类型:文本/html。但是在我的代码中,当我检查标头是否具有 Content-Type 时,它​​不会显示任何内容。如果我删除 if 循环,那么它会打印与我想要抓取的特定 url 相关的每个链接及其响应标头..

public class MyCrawler extends WebCrawler {

    Pattern filters = Pattern.compile(".*(\\.(css|js|bmp|gif|jpe?g"
            + "|png|tiff?|mid|mp2|mp3|mp4" + "|wav|avi|mov|mpeg|ram|m4v|pdf"
            + "|rm|smil|wmv|swf|wma|zip|rar|gz))$");


    /*
    Pattern filters = Pattern.compile("(\\.(html))");
*/
    public MyCrawler() {
    }

    public boolean shouldVisit(WebURL url) {
        String href = url.getURL().toLowerCase();
        //System.out.println("Href: " +href);
        /*
        if (filters.matcher(href).matches()) {
            return false;
        }*/
        if (href.startsWith("http://www.somehost.com/")) {
            return true;
        }
        return false;
    }

    public void visit(Page page) {

        int docid = page.getWebURL().getDocid();

        String url = page.getWebURL().getURL();         
        String text = page.getText();
        List<WebURL> links = page.getURLs();
        int parentDocid = page.getWebURL().getParentDocid();


        //HttpGet httpget = new HttpGet(url);


        try {
            URL url1 = new URL(url);
            URLConnection connection = url1.openConnection();

            Map responseMap = connection.getHeaderFields();
      for (Iterator iterator = responseMap.keySet().iterator(); iterator.hasNext();) 
     {
                String key = (String) iterator.next();
                if(key==("Content-Type")) //(Anything wrong with this if loop)
                {
                System.out.print(key + " = ");

                List values = (List) responseMap.get(key);
                for (int i = 0; i < values.size(); i++) {
                    Object o = values.get(i);
                    System.out.print(o + ", ");
                }
                System.out.println("");
System.out.println(url1);
                }

            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        //System.out.println("Docid: " + docid);
        //System.out.println("URL: " + url);
        //System.out.println("Text length: " + text.length());
        //System.out.println("Number of links: " + links.size());
        //System.out.println("Docid of parent page: " + parentDocid);
        System.out.println("=============");
    }   
}

I am trying to get all the url's that have header as Content-Type:text/html so I am checking the response header of each url and If they have content-type: text/html, then I want to print that url that has content-type:text/html. But in my code when I am checking that if the header has Content-Type, it is not displaying anything.. And If I remove the if loop then it prints every link related to that particular url that I want to crawl and their response header..

public class MyCrawler extends WebCrawler {

    Pattern filters = Pattern.compile(".*(\\.(css|js|bmp|gif|jpe?g"
            + "|png|tiff?|mid|mp2|mp3|mp4" + "|wav|avi|mov|mpeg|ram|m4v|pdf"
            + "|rm|smil|wmv|swf|wma|zip|rar|gz))$");


    /*
    Pattern filters = Pattern.compile("(\\.(html))");
*/
    public MyCrawler() {
    }

    public boolean shouldVisit(WebURL url) {
        String href = url.getURL().toLowerCase();
        //System.out.println("Href: " +href);
        /*
        if (filters.matcher(href).matches()) {
            return false;
        }*/
        if (href.startsWith("http://www.somehost.com/")) {
            return true;
        }
        return false;
    }

    public void visit(Page page) {

        int docid = page.getWebURL().getDocid();

        String url = page.getWebURL().getURL();         
        String text = page.getText();
        List<WebURL> links = page.getURLs();
        int parentDocid = page.getWebURL().getParentDocid();


        //HttpGet httpget = new HttpGet(url);


        try {
            URL url1 = new URL(url);
            URLConnection connection = url1.openConnection();

            Map responseMap = connection.getHeaderFields();
      for (Iterator iterator = responseMap.keySet().iterator(); iterator.hasNext();) 
     {
                String key = (String) iterator.next();
                if(key==("Content-Type")) //(Anything wrong with this if loop)
                {
                System.out.print(key + " = ");

                List values = (List) responseMap.get(key);
                for (int i = 0; i < values.size(); i++) {
                    Object o = values.get(i);
                    System.out.print(o + ", ");
                }
                System.out.println("");
System.out.println(url1);
                }

            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        //System.out.println("Docid: " + docid);
        //System.out.println("URL: " + url);
        //System.out.println("Text length: " + text.length());
        //System.out.println("Number of links: " + links.size());
        //System.out.println("Docid of parent page: " + parentDocid);
        System.out.println("=============");
    }   
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七色彩虹 2024-11-26 07:37:43

key 变量包含:

Content-Type=[text/html; charset=ISO-8859-1]

因此不能用 ==.equals("Content-Type") 捕获

如果您尝试运行以下代码,看看它打印出什么

URLConnection connection = url1.openConnection();

Map responseMap = connection.getHeaderFields();
Iterator iterator = responseMap.entrySet().iterator();
while (iterator.hasNext())
{
    String key = iterator.next().toString();
    if (key.contains("Content-Type"))
    {
        System.out.println(key);
        // Content-Type=[text/html; charset=ISO-8859-1]
        if (filters.matcher(key) != null){
            System.out.println(url1);
            // http://google.com
        }
    }
}

这是输出:

Content-Type=[text/html; charset=ISO-8859-1]
http://google.com

看起来您也可以只使用一个 if 语句,如下所示:

while (iterator.hasNext())
{
    String key = iterator.next().toString();
    if (key.contains("text/html"))
    {
        System.out.println(url1);
        // http://google.com
    }
}

BTW string Comparison in Java 非常直观,一直让我着迷!

The key variable contains:

Content-Type=[text/html; charset=ISO-8859-1]

and therefor can't be caught with == or .equals("Content-Type")

If you try to run the following code, see what it prints out

URLConnection connection = url1.openConnection();

Map responseMap = connection.getHeaderFields();
Iterator iterator = responseMap.entrySet().iterator();
while (iterator.hasNext())
{
    String key = iterator.next().toString();
    if (key.contains("Content-Type"))
    {
        System.out.println(key);
        // Content-Type=[text/html; charset=ISO-8859-1]
        if (filters.matcher(key) != null){
            System.out.println(url1);
            // http://google.com
        }
    }
}

Here is the output:

Content-Type=[text/html; charset=ISO-8859-1]
http://google.com

It looks like you could also just do with one if statement as following:

while (iterator.hasNext())
{
    String key = iterator.next().toString();
    if (key.contains("text/html"))
    {
        System.out.println(url1);
        // http://google.com
    }
}

BTW string comparison in Java is very intuitive, gets me all the time!

野の 2024-11-26 07:37:43

对于字符串比较,请使用 .equals()

For string comparison, use .equals().

硬不硬你别怂 2024-11-26 07:37:43

它应该与

if (key != null && key.equals("Content-Type"))

It should work with

if (key != null && key.equals("Content-Type"))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文