Java 如何使用WebGrude刮取搜索结果?

Java 如何使用WebGrude刮取搜索结果?,java,web-scraping,Java,Web Scraping,我最近用它从网页上刮取一些内容。然后我试着从e-bay中获取一些搜索结果。这是我尝试过的 @Page("http://www.ebay.com/sch/{0}") public class PirateBay { public static void main(String[] args) { //Search calls Browser, which loads the page on a PirateBay instance PirateBay se

我最近用它从网页上刮取一些内容。然后我试着从e-bay中获取一些搜索结果。这是我尝试过的

@Page("http://www.ebay.com/sch/{0}")
public class PirateBay {

    public static void main(String[] args) {
        //Search calls Browser, which loads the page on a PirateBay instance
        PirateBay search = PirateBay.search("iPhone");

        while (search != null) {
             search.magnets.forEach(System.out::println);
            search = search.nextPage();
        }
    }

    public static PirateBay search(String term) {
        return Browser.get(PirateBay.class, term);
    }

    private PirateBay() {
    }

    /*
* This selector matches all magnet links. The result is added to this String list.
* The default behaviour is to use the rendered html inside the matched tag, but here
* we want to use the href value instead.
*/
    @Selector(value = "#ResultSetItems a[href*=magnet]", attr = "href")
    public List<String> magnets;

/*
* This selector matches a link to the next page result, wich can be mapped to a PirateBay instance.
* The Link next gets the page on the href attribute of the link when method visit is called.
*/
    @Selector("a:has(img[alt=Next])")
    private Link<PirateBay> next;

    public PirateBay nextPage() {
        if (next == null)
            return null;
        return next.visit();
        }
    }
@页(“http://www.ebay.com/sch/{0}")
公共类盗版{
公共静态void main(字符串[]args){
//搜索调用浏览器,浏览器在eBay实例上加载页面
海盗易趣搜索=海盗易趣搜索(“iPhone”);
while(搜索!=null){
search.magnetics.forEach(System.out::println);
search=search.nextPage();
}
}
公共静态搜索(字符串术语){
返回Browser.get(biratebay.class,term);
}
私人盗版{
}
/*
*此选择器匹配所有磁铁链接。结果将添加到此字符串列表中。
*默认行为是在匹配的标记内使用呈现的html,但此处
*我们希望改用href值。
*/
@选择器(value=“#ResultSetItems a[href*=magnet]”,attr=“href”)
公开名单;
/*
*此选择器匹配指向下一页结果的链接,该结果可以映射到eBay实例。
*当调用方法visit时,链接接下来将获取链接的href属性上的页面。
*/
@选择器(“a:has(img[alt=Next]))
私有链接下一步;
公共盗版eBay下一页(){
if(next==null)
返回null;
返回next.visit();
}
}
但结果是空的。如何使用此选项刮取搜索结果?

选择器“#ResultSetItems a[href*=magnet]”选择href属性值上带有字符串“magnet”的链接

在这里,您可以阅读更多有关心房肌选择器的信息:

您需要的是“#ResultSetItems h3.lvtitle a”

为了测试选择器,有一个很好的repl使用Jsoup,与webgrude使用的库相同,选择器“#ResultSetItems a[href*=magnet]”选择href属性值上有字符串“magnet”的链接

在这里,您可以阅读更多有关心房肌选择器的信息:

您需要的是“#ResultSetItems h3.lvtitle a”

为了测试选择器,有一个很好的repl,它使用Jsoup,与webgrude使用的库相同