我想获取所有图片的所有信息,有没有简单的方法在Mac上使用bash?我想获得如下数据:
"product": "8020"
"simage": "/uploadfile/201281616171259157_.GIF"
"image": "/uploadfile/201281616171259157.GIF"
"name": "Taipei 101"
"product": "8019"
"simage": "/uploadfile/201432010288118198_.jpg"
"image": "/uploadfile/201432010288118198.jpg"
"name": "TianTan"
这不起作用,我也需要产品和名称等,src属性中没有......
baseurl=$(echo $url | egrep -o "https?://[a-z.]+")
curl --silent $url | egrep -o "src=[^>]*(\.jpg|\.gif|\.png)" | sed 's/src=\"\(.*\)/\1/g' > /tmp/$$.list
sed -i "s|^/|$baseurl/|" /tmp/$$.list
while read filename;
do
curl -s -O "$baseurl/$filename"
done < /tmp/$$.list
product.asp?cxsort = 10001的网站内容
....
<ul id="small" >
<li><a href="product.asp?cxsort=10001">Military1</a></li>
<li><a href="product.asp?cxsort=10021">Military2</a></li>
<li><a href="product.asp?cxsort=10101">Military3</a></li>
....
</ul>
....
<table cellpadding="0" cellspacing="0">
<tr>
<td>Product:8020</td>
<td><div class="set"><img src="/uploadfile/201281616171259157_.GIF" width="94" height="69" style="display:block" class="/uploadfile/201281616171259157.GIF" alt="TianTan" /></div></td>
</tr>
</table>
....
<table cellpadding="0" cellspacing="0">
<tr>
<td>Product:8019</td>
<td><div class="Set"><img src="/uploadfile/201432010288118198_.jpg" width="94" height="69" style="display:block" class="/uploadfile/201432010288118198.jpg" alt="Taipei 101" /></div></td>
</tr>
</table>
....
答案 0 :(得分:2)
你可以试试这个:
sed -n '
/Product/ {
s/[ \t]*<[^>]*>//g
s/Product:\([0-9]*\)/"product": "\1"/p
n
s/.*img *src="\([^"]*\)".*class="\([^"]*\).*alt="\([^"]*\).*/"simage": "\1"\n"image": "\2"\n"name": "\3"\n/p
}
' file.html
它适用于您的示例,如果与产品和图像相关的代码总是以相同的方式构建,则应该在您的html上执行操作。
但是像python中的BeautifulSoup这样的网络抓取库将是更好的选择。
BeautifulSoup python代码如下所示:
from bs4 import BeautifulSoup
f = file('file.html', 'r')
soup = BeautifulSoup(f)
all_img = soup.find_all('img')
for img in all_img:
print '%s : %s' % (img['alt'], img['src'])