分享一段源码挖掘IMDb电影信息(电影源代码)

deer332025-02-01技术文章51

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 '<h5>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 '<h1 class="findHeader">No ' $tempout)"
 
if [ ! -z "$fail" ] ;then
 echo "Failed:no results foun for $1"
 exit 1
elif [ ! -z "$(grep '<h1 class="findHeader">Displaying' $tempout)" ] ;then
 grep --color=never '/title/tt' $tempout | \
 sed 's/</\
 </g' | \
 grep -vE '(.png|.jpg|>[ ]*$)' |\
 grep -A 1 "a href=" | \
 grep -v '^--$' | \
 sed 's/<a href="\/title\/tt//g;s/<\/a>//' | \
 awk '(NR %2 ==1){title=$0} (NR %2 ==0){print title " " $0}' | \
 sed 's/\/.*>/: /'|\
 sort
fi
exit 0
</pre><p>大家可以复制下来运行一下,不懂的话直接私信小编,或者加群讨论!!一起学习一起进步!!!</p><p>我们是做教育的,希望大家能够学到知识,这个一直是我们的初衷!!</p><p>记住噢,加群讨论噢,不懂的可以问,也有人回答的。</p></div>                </div>
                                            
        </div>
    </div>
<div class="block">
	<div class="posttitle"><h4>相关文章</h4></div>
	<div class="relatecon">
<div class="relatelist"><a href="http://www.deer33.com/post/750.html" title="图解--《源代码》(源代码示例)">图解--《源代码》(源代码示例)</a><span class="posttime">2025-02-01</span></div><div class="relatelist"><a href="http://www.deer33.com/post/751.html" title="推荐电影86《源代码》时空循环硬壳烧脑片,男主不停穿越平行时空">推荐电影86《源代码》时空循环硬壳烧脑片,男主不停穿越平行时空</a><span class="posttime">2025-02-01</span></div><div class="relatelist"><a href="http://www.deer33.com/post/752.html" title="[电影摘要]《源代码》(电影源代码结局)">[电影摘要]《源代码》(电影源代码结局)</a><span class="posttime">2025-02-01</span></div><div class="relatelist"><a href="http://www.deer33.com/post/756.html" title="科幻电影推荐《源代码》:大脑载入8分钟记忆,创造出平行世界">科幻电影推荐《源代码》:大脑载入8分钟记忆,创造出平行世界</a><span class="posttime">2025-02-01</span></div><div class="relatelist"><a href="http://www.deer33.com/post/758.html" title="浅析《源代码》电影(源代码电影介绍)">浅析《源代码》电影(源代码电影介绍)</a><span class="posttime">2025-02-01</span></div><div class="relatelist"><a href="http://www.deer33.com/post/762.html" title="《源代码》电影解说词(1)(电影源代码好看吗)">《源代码》电影解说词(1)(电影源代码好看吗)</a><span class="posttime">2025-02-01</span></div>	</div>
</div>
</div>
<div class="sidebar fixed">
    
<dl id="divCatalog" class="sidebox">
    <dt class="sidetitle">网站分类</dt>    <dd>
        				<ul><li><a title="技术文章" href="http://www.deer33.com/?cate=1">技术文章</a></li>
</ul>
		    </dd>
</dl><dl id="divTags" class="sidebox">
    <dt class="sidetitle">标签列表</dt>    <dd>
        				<ul><li><a title="数据库设计工具有哪些" href="http://www.deer33.com/tags-2.html">数据库设计工具有哪些<span class="tag-count"> (34)</span></a></li>
<li><a title="网上商城项目完整源码" href="http://www.deer33.com/tags-3.html">网上商城项目完整源码<span class="tag-count"> (34)</span></a></li>
<li><a title="系统管理系统" href="http://www.deer33.com/tags-6.html">系统管理系统<span class="tag-count"> (43)</span></a></li>
<li><a title="round向上取整公式怎么用" href="http://www.deer33.com/tags-7.html">round向上取整公式怎么用<span class="tag-count"> (57)</span></a></li>
<li><a title="htmlinput属性" href="http://www.deer33.com/tags-11.html">htmlinput属性<span class="tag-count"> (46)</span></a></li>
<li><a title="html5下载官方网站" href="http://www.deer33.com/tags-12.html">html5下载官方网站<span class="tag-count"> (48)</span></a></li>
<li><a title="python正则表达式使用实例" href="http://www.deer33.com/tags-15.html">python正则表达式使用实例<span class="tag-count"> (35)</span></a></li>
<li><a title="程序员需要考哪些证书" href="http://www.deer33.com/tags-16.html">程序员需要考哪些证书<span class="tag-count"> (34)</span></a></li>
<li><a title="程序员基础知识" href="http://www.deer33.com/tags-17.html">程序员基础知识<span class="tag-count"> (33)</span></a></li>
<li><a title="源代码电影在线播放" href="http://www.deer33.com/tags-26.html">源代码电影在线播放<span class="tag-count"> (37)</span></a></li>
<li><a title="java我的世界启动器" href="http://www.deer33.com/tags-29.html">java我的世界启动器<span class="tag-count"> (44)</span></a></li>
<li><a title="手机数控编程软件免费" href="http://www.deer33.com/tags-36.html">手机数控编程软件免费<span class="tag-count"> (56)</span></a></li>
<li><a title="如何编程序" href="http://www.deer33.com/tags-37.html">如何编程序<span class="tag-count"> (35)</span></a></li>
<li><a title="手机编程游戏软件" href="http://www.deer33.com/tags-43.html">手机编程游戏软件<span class="tag-count"> (35)</span></a></li>
<li><a title="简单学生的网页代码" href="http://www.deer33.com/tags-44.html">简单学生的网页代码<span class="tag-count"> (35)</span></a></li>
<li><a title="sql必知必会" href="http://www.deer33.com/tags-56.html">sql必知必会<span class="tag-count"> (36)</span></a></li>
<li><a title="二次函数公式大全表格" href="http://www.deer33.com/tags-59.html">二次函数公式大全表格<span class="tag-count"> (36)</span></a></li>
<li><a title="jquery事件处理" href="http://www.deer33.com/tags-62.html">jquery事件处理<span class="tag-count"> (34)</span></a></li>
<li><a title="jquery ui下载" href="http://www.deer33.com/tags-63.html">jquery ui下载<span class="tag-count"> (36)</span></a></li>
<li><a title="jquery是什么库" href="http://www.deer33.com/tags-64.html">jquery是什么库<span class="tag-count"> (35)</span></a></li>
<li><a title="网站免费源码大全下载" href="http://www.deer33.com/tags-153.html">网站免费源码大全下载<span class="tag-count"> (34)</span></a></li>
<li><a title="常用控件" href="http://www.deer33.com/tags-254.html">常用控件<span class="tag-count"> (35)</span></a></li>
<li><a title="wordpress网站建设" href="http://www.deer33.com/tags-329.html">wordpress网站建设<span class="tag-count"> (34)</span></a></li>
<li><a title="反函数和原函数关系" href="http://www.deer33.com/tags-335.html">反函数和原函数关系<span class="tag-count"> (34)</span></a></li>
<li><a title="sqlserver" href="http://www.deer33.com/tags-360.html">sqlserver<span class="tag-count"> (34)</span></a></li>
</ul>
		    </dd>
</dl><dl id="divComments" class="sidebox">
    <dt class="sidetitle">最新留言</dt>    <dd>
        				<ul></ul>
		    </dd>
</dl></div>                            </div>
        </div>
    </div>
</div>
<div class="footer">
	<div class="fademask"></div>
    <div class="wrap">
        <h3><a href="https://beian.miit.gov.cn/" target="_blank">蜀ICP备2024111239号-17
</a></h3>
        <!--<h4>Powered By <a href="https://www.zblogcn.com/" title="Z-BlogPHP 1.7.3 Build 173295" target="_blank" rel="noopener norefferrer">Z-BlogPHP</a>. Theme by <a href="https://www.toyean.com/" target="_blank" title="拓源网">TOYEAN</a>.</h4>-->
        
    </div>
</div>
<div class="edgebar">
</div></body>
</html><!--89.00 ms , 9 queries , 3843kb memory , 0 error-->