如何获取站点中所有图像的所有信息
How to get all information with all images in site
我想获取所有图片的所有信息,有什么简单的方法可以在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"
这不行,我还需要product和name等等,src属性里没有...
baseurl=$(echo $url | egrep -o "https?://[a-z.]+")
curl --silent $url | egrep -o "src=[^>]*(\.jpg|\.gif|\.png)" | sed 's/src=\"\(.*\)//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>
....
你可以试试这个:
sed -n '
/Product/ {
s/[ \t]*<[^>]*>//g
s/Product:\([0-9]*\)/"product": ""/p
n
s/.*img *src="\([^"]*\)".*class="\([^"]*\).*alt="\([^"]*\).*/"simage": ""\n"image": ""\n"name": ""\n/p
}
' file.html
它适用于您的示例,并且如果与产品和图像相关的代码始终以相同的方式构建,则应该对您的 html 起作用。
但是 python 中的 BeautifulSoup 这样的网络抓取库会是更好的选择。
A 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'])
我想获取所有图片的所有信息,有什么简单的方法可以在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"
这不行,我还需要product和name等等,src属性里没有...
baseurl=$(echo $url | egrep -o "https?://[a-z.]+")
curl --silent $url | egrep -o "src=[^>]*(\.jpg|\.gif|\.png)" | sed 's/src=\"\(.*\)//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>
....
你可以试试这个:
sed -n '
/Product/ {
s/[ \t]*<[^>]*>//g
s/Product:\([0-9]*\)/"product": ""/p
n
s/.*img *src="\([^"]*\)".*class="\([^"]*\).*alt="\([^"]*\).*/"simage": ""\n"image": ""\n"name": ""\n/p
}
' file.html
它适用于您的示例,并且如果与产品和图像相关的代码始终以相同的方式构建,则应该对您的 html 起作用。
但是 python 中的 BeautifulSoup 这样的网络抓取库会是更好的选择。
A 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'])