Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JSOUP登录,然后解析&;在HTML字符串中查找价格_Java_Web Crawler_Jsoup_Screen Scraping - Fatal编程技术网

Java JSOUP登录,然后解析&;在HTML字符串中查找价格

Java JSOUP登录,然后解析&;在HTML字符串中查找价格,java,web-crawler,jsoup,screen-scraping,Java,Web Crawler,Jsoup,Screen Scraping,我基本上已经搞定了这个计划。我只需要从这个HTML字符串中提取价格:您的价格:$23.51您节省:$11.48我只需要该字符串中的“23.51”,作为您的价格列出 我更新了代码以反映我目前正在使用的内容。我不确定下一步是什么 我不知道在哪里添加解析。我需要它从电子表格中获取URL,然后将价格数据放入相应的列(price_列) 这是我的密码,多亏了Krystian public class Scraper { private static final int URL_COLUMN = 2; /

我基本上已经搞定了这个计划。我只需要从这个HTML字符串中提取价格:
  • 您的价格:$23.51您节省:$11.48
  • 我只需要该字符串中的“23.51”,作为您的价格列出

    我更新了代码以反映我目前正在使用的内容。我不确定下一步是什么

    我不知道在哪里添加解析。我需要它从电子表格中获取URL,然后将价格数据放入相应的列(price_列)

    这是我的密码,多亏了Krystian

    public class Scraper {
    
    
    private static final int URL_COLUMN = 2; // Column C
    private static final int SKU_COLUMN = 3; // Column D
    private static final int SALE_COLUMN = 4;// Column E
    private static final int PRICE_COLUMN = 5; //Column F
    
    public static void main(final String[] args) throws Exception {
    
        Workbook originalWorkbook = Workbook.getWorkbook(new File("C:/Users/MSI/Desktop/original.xls"));
        WritableWorkbook workbook = Workbook.createWorkbook(new File("C:/Users/MSI/Desktop/updated.xls"), originalWorkbook);
        originalWorkbook.close();
        WritableSheet sheet = workbook.getSheet(0);
        int currentRow = 1;
        Cell cell;
        while (!(cell = sheet.getCell(URL_COLUMN, currentRow)).getType().equals(CellType.EMPTY)) {
            String url = cell.getContents();
            System.out.println("Parsing URL: " + url);
            String SKU = parseUrlWithJsoupAndGetProductSKU(url);
            String price = parseUrlWithJsoupAndGetProductPrice(url);
            String sale= parseUrlWithJsoupAndGetSale(url);
            System.out.println("SKU: " + SKU);
            System.out.println("Regular price: " + price);
            System.out.println("Sale price: " + sale);
            Label cellWithSKU = new Label(SKU_COLUMN, currentRow, SKU);
            sheet.addCell(cellWithSKU);
            Label cellWithSale = new Label(SALE_COLUMN, currentRow, sale);
            sheet.addCell(cellWithSale);
            Label cellWithPrice = new Label(PRICE_COLUMN, currentRow, price);
            sheet.addCell(cellWithPrice);
            currentRow++;
        }
        workbook.write();
        workbook.close();
    
    }
    
    private static String parseUrlWithJsoupAndGetProductSKU(String url) throws IOException {
        try {
            Document doc = Jsoup.connect(url).userAgent("Mozilla").get();
            return doc.select("#product_id_num").text();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    private static String parseUrlWithJsoupAndGetSale(String url) throws IOException {
        try {
            Document doc = Jsoup.connect(url).userAgent("Mozilla").get();
            return doc.select("#NA").text();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    private static String parseUrlWithJsoupAndGetProductPrice(String url) throws IOException {
        try {
            Document doc = Jsoup.connect(url).userAgent("Mozilla").get();
            System.out.println(getPrice(doc));
            return doc.select("#price").text();
            private static String price(Document doc) {
                // select all <li> inside <ul class="small">
                Elements liElements = doc.select("ul.small>li");
                for (Element li : liElements) {
                    // find "List Price:"
                    if (li.text().contains("List Price:")) {
                        // remove <b> with contents
                        li.select("b").remove();
                        // there's only price left in <li>
                        return li.text();
                    }
                }
                return "not found";
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    公共类刮板{
    私有静态final int URL_COLUMN=2;//列C
    私有静态final int SKU_COLUMN=3;//列D
    私有静态final int SALE_COLUMN=4;//列E
    私有静态final int PRICE_COLUMN=5;//第F列
    公共静态void main(最终字符串[]args)引发异常{
    工作簿originalWorkbook=Workbook.getWorkbook(新文件(“C:/Users/MSI/Desktop/original.xls”);
    WritableWorkbook工作簿=工作簿.createWorkbook(新文件(“C:/Users/MSI/Desktop/updated.xls”),原始工作簿);
    原始工作簿。关闭();
    WritableSheet=workbook.getSheet(0);
    int currentRow=1;
    细胞;
    而(!(cell=sheet.getCell(URL_列,currentRow)).getType()等于(CellType.EMPTY)){
    字符串url=cell.getContents();
    System.out.println(“解析URL:+URL”);
    字符串SKU=parseUrlWithJsoupAndGetProductSKU(url);
    字符串price=parseUrlWithJsoupAndGetProductPrice(url);
    字符串sale=parseUrlWithJsoupAndGetSale(url);
    系统输出打印项次(“SKU:+SKU”);
    System.out.println(“常规价格:“+价格”);
    System.out.println(“销售价格:+销售);
    Label cellWithSKU=新标签(SKU_列,当前行,SKU);
    表1.addCell(CellWithKu);
    Label cellWithSale=新标签(SALE_列,currentRow,SALE);
    表1.addCell(cellWithSale);
    标签cellWithPrice=新标签(价格列、当前行、价格);
    表.添加单元格(单元格带价格);
    currentRow++;
    }
    workbook.write();
    workbook.close();
    }
    私有静态字符串parseUrlWithJsoupAndGetProductSKU(字符串url)引发IOException{
    试一试{
    Document doc=Jsoup.connect(url.userAgent(“Mozilla”).get();
    退货单。选择(“#产品id_数量”).text();
    }捕获(IOE异常){
    e、 printStackTrace();
    }
    返回null;
    }
    私有静态字符串parseUrlWithJsoupAndGetSale(字符串url)引发IOException{
    试一试{
    Document doc=Jsoup.connect(url.userAgent(“Mozilla”).get();
    返回单据。选择(“#NA”).text();
    }捕获(IOE异常){
    e、 printStackTrace();
    }
    返回null;
    }
    私有静态字符串parseUrlWithJsoupAndGetProductPrice(字符串url)引发IOException{
    试一试{
    Document doc=Jsoup.connect(url.userAgent(“Mozilla”).get();
    System.out.println(getPrice(doc));
    退货单。选择(“#价格”).text();
    私有静态字符串价格(单据){
    //在
      元素liElements=doc.select(“ul.small>li”); 对于(元素li:元素){ //查找“标价”: 如果(li.text()包含(“标价:”){ //随内容物一起移除 li.选择(“b”).删除(); //
    • 返回li.text(); } } 返回“未找到”; } }捕获(IOE异常){ e、 printStackTrace(); } 返回null; }
    注释中的解释:

    public static void main(final String[] args) throws IOException {
        Document doc = Jsoup
                .parse("<ul class=\"small\">    <li class=\"\"><b class=\"\">Description:</b> . (Import)</li> <li class=\"\"><b class=\"\">List Price:</b> $123</li> <li class=\"\"><b class=\"\">Your Price:</b> $***&nbsp; &nbsp; <b class=\"\">You Save:</b> $***</li> <li class=\"stockStatus\"><b class=\"\">Stock Status:</b> 1</li> <h3 class=\"additionalInfo\"><span class=\"\">Additional Information</span></h3> <li class=\"additional\">Uses extension part# ***</li> <li class=\"additional\">Replaces GM# ***</li> </ul> ");
        System.out.println(getPrice(doc));
    }
    
    private static String getPrice(Document doc) {
        // select all <li> inside <ul class="small">
        Elements liElements = doc.select("ul.small>li");
        for (Element li : liElements) {
            // find "List Price:"
            if (li.text().contains("List Price:")) {
                // remove <b> with contents
                li.select("b").remove();
                // there's only price left in <li>
                return li.text();
            }
        }
        return "not found";
    }
    
    publicstaticvoidmain(最终字符串[]args)引发IOException{
    文档doc=Jsoup
    .parse(“
    • 标价:$123
    • 您的价格:$***您保存:$***
    • 库存状态:1
    • 附加信息
    • 使用扩展部件
    • “>替换通用汽车 元素liElements=doc.select(“ul.small>li”); 对于(元素li:元素){ //查找“标价”: 如果(li.text()包含(“标价:”){ //随内容物一起移除 li.选择(“b”).删除(); //
    • 返回li.text(); } } 返回“未找到”; }
    您能为所选元素提供更多上下文吗?
  • 选择它是不够的。
    • 说明:.(导入)
    • 标价:$EA
    • 您的价格:$***您保存:$***
    • 库存状态:1
    • 其他信息
    • 使用扩展部分#***
    • 替换GM#***
    Krystian,我们应该按特定字符串进行过滤吗?或者我该如何解决此问题?错误:(47,19)java:类、接口或枚举预期我在新行中会出现错误,以“void”开头.我现在正试图弄清楚,我想知道这样做是否还能输出到电子表格“更新”中。看起来这段代码没有使用URL,所以我只是将其添加到了前面的代码中,但我当然得到了一个错误,即主字符串已经定义。这是如何从
    文档
    (您的HTML代码)获取价格的独立示例因此,每个人都可以在不使用以前的代码的情况下运行它。您不需要url,因为您已经下载了页面并将其解析为
    doc
    对象。您应该在
    parseUrlWithJsoupAndGetProductPrice(url)
    方法中使用此代码。我仍然对此感到有点困惑。您是说当我运行pro时