Bash 从ISBN和Shell脚本获取书籍详细信息

Bash 从ISBN和Shell脚本获取书籍详细信息,bash,shell,sed,grep,Bash,Shell,Sed,Grep,假设我有一个ISBN值列表: 9781887902694 9780072227109 9780672323843 9780782121797 9781565924031 9780735713338 9780735713338 ... 如何使用shell脚本/bash检索标题、发布日期、作者和发布者(从bookfinder4u.com这样的网站)?我是bash新手,所以我不知道如何继续。#/bin/bash #!/bin/bash if [ -z "$1" ] ; then echo "Usag

假设我有一个ISBN值列表:

9781887902694
9780072227109
9780672323843
9780782121797
9781565924031
9780735713338
9780735713338
...
如何使用shell脚本/bash检索标题、发布日期、作者和发布者(从bookfinder4u.com这样的网站)?我是bash新手,所以我不知道如何继续。

#/bin/bash
#!/bin/bash
if [ -z "$1" ] ; then echo "Usage: $0 <ISBN number>" ; exit 1 ; fi
curl -sL 'http://www.bookfinder4u.com/IsbnSearch.aspx?isbn='$1'&mode=direct'
如果[-z“$1”];然后回显“用法:$0”;出口1;fi curl-sL'http://www.bookfinder4u.com/IsbnSearch.aspx?isbn=“$1”&模式=直接”

这将使您获得页面,但是用grep和sed解析该响应看起来会非常混乱。如果您知道一个将返回JSON或XML的API,这会更容易。

如果您能够运行
php
,您可以使用:

bookDetails.php

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
//you may want to un-comment the code below to make sure your script doesn't timeout
//set_time_limit(0); 
//ignore_user_abort(true);
libxml_use_internal_errors(true);

//get all the isbn numbers from a txt file
$isbns = file("isbn.txt", FILE_IGNORE_NEW_LINES);

//lopp all the isbn's
foreach($isbns as $isbn){

    $html = file_get_contents("http://www.bookfinder4u.com/IsbnSearch.aspx?isbn=$isbn");
    $dom = new DomDocument();
    $dom->loadHtml($html);
    $xpath = new DomXpath($dom);

    $title =  $xpath->query('//*[@class="t9"]')->item(1)->nodeValue;
    $author =  $xpath->query('//*[@class="t9"]')->item(2)->nodeValue;
    $pubisherFormat =  $xpath->query('//*[@id="format_pub_listprice"]')->item(0)->c14n();
    $matches = preg_split('%</br>%', $pubisherFormat);
    $publisher = strip_tags($matches[0]);
    $format = strip_tags($matches[1]);
    $price =  $xpath->query('//*[@class="t8"]')->item(1)->nodeValue;
    preg_match_all('/List price:\s*?(.*?[\d\.]+)/', $price, $price, PREG_PATTERN_ORDER);
    $price = $price[1][0];

    echo $title."\n";
    echo $author."\n";
    echo $publisher."\n";
    echo $format."\n";
    echo $price."\n\n";

}
输出将是:

Javascript: Concepts & Techniques; Programming Interactive Web Sites
By: Tina Spain McDuffie
Publisher: Franklin Beedle & Assoc - 2003-01
Format: Paperback
EUR 48.32

J2ME: The Complete Reference
By: James Keogh
Publisher: McGraw-Hill - 2003-02-27
Format: Paperback
EUR 57.94

Sams Teach Yourself J2ee in 21 Days with CDROM (Sams Teach Yourself...in 21 Days)
By: Martin Bond Dan Haywood Peter Roxburgh
Publisher: Sams - 2002-04
Format: Paperback
EUR 43.92

外壳运行

php bookDetails.php

我相信我可以用curl,不?不要用bash;如果你真的得到了一些工作,它将是一个怪物,很可能是脆弱的。Python、Perl或Ruby将能够轻松处理。另外,请注意,您所引用的网站可能会阻止或阻止许多请求,因为它主要是一个图书销售网站,而不是ISBN引用。进一步介绍@msw所说的:Shell脚本非常适合处理操作系统/本地网络内的业务。如果您需要操作文件、转换字符串、完成搜索、完成排序、建立连接等,那么shell脚本就是一种选择。一旦您离开操作系统领域,shell脚本就会失去很多功能。更直接地说,bash没有真正的工具去上网做这样的事情,而其他语言如Python有几个库。我可以使用curl获取网站源代码,然后使用脚本将isbn输入到正确的区域?你知道我该怎么做吗?当curl是Python的一部分时,就不用麻烦了,因为它可以帮助你从HTML中提取内容。
php bookDetails.php