Python能抓取网页上的信息,当然bash也可以,今天小编就拿bash脚本来挖掘IMDb中的电影信息噢!!挺好玩的!!!
我们是做教育的,希望大家能够学到知识,这个一直是我们的初衷!!
电影网站https://www.imdb.com,这个网站我自己登入上去看了看,确实可以登入,并不需要挂vpn,可惜的是英文。
因为代码太长了,我分屏了一下,大伙可以看行号码,不过我会把源码分享出去。
代码里用到了大量的sed和grep,这个sed我之前有分享,改天找个机会说说grep的使用。
写好了运行一下吧,在脚本后面lawrence of arabia是电影的名字。
你看吧!出来一系列的电影了!当然你的英文要好!!
那我现在就分享一下代码吧!!
#!/bin/bash
#Date 2019-09-17
PATH=/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
titleurl="http://www.imdb.com/title/tt"
imdburl="http://www.imdb.com/find?s=tt&exact=true&ref_=fn_tt_ex&q="
tempout="/tmp/moviedata.$"
summarize_film()
{
grep "" $tempout | sed 's/<[^>]*>//g;s/(more)//'
grep --color=never -A2 'Plot:' $tempout | tail -1 |\
cut -d\ -f1 | fmt | sed 's/^/ /'
exit 0
}
trap "rm -f $tempout" 0 1 15
if [ $# -eq 0 ] ; then
echo "Usage:$0 {movie title | movie ID}" >&2
exit 1
fi
######
nodigits="$(echo $1 | sed 's/[[:digit:]]*//g')"
if [ $# -eq 1 -a -z "$nodigits" ] ;then
lynx -source "$titleurl$1/combined" > $tempout
summarize_film
exit 0
fi
########
fixedname="$(echo $@ | tr ' ' '+')"
url="$imdburl$fixedname"
lynx -source $imdburl$fixedname > $tempout
fail="$(grep --color --color=never 'No ' $tempout)"
if [ ! -z "$fail" ] ;then
echo "Failed:no results foun for $1"
exit 1
elif [ ! -z "$(grep 'Displaying' $tempout)" ] ;then
grep --color=never '/title/tt' $tempout | \
sed 's/</\
[ ]*$)' |\
grep -A 1 "a href=" | \
grep -v '^--