2016-03-23 74 views
0

我试图用imdb包编写一个简单的python程序从他们的数据库中提取电影信息,但我不知道为什么代码返回空列表。我的猜测是,我从网站中提取网址信息(通过使用(。*?))的方式是错误的。我想从网页中提取一个url链接。这是代码。谢谢!python crawler extract url not working

import urllib 
import re 
import imdb 
imdb_access = imdb.IMDb() 

top_num = 5 

movie_list = ["The Matrix","The Matrix","The Matrix","The Matrix","The Matrix"] 


for x in xrange(0,top_num): 
    contain = imdb_access.search_movie(movie_list[x]) 

    ID = contain[0].movieID #str type 

    htmltext = (urllib.urlopen("http://www.imdb.com/title/tt0133093/?ref_=nv_sr_1")).read() 
    # a pattern in the website 
    regex = regex = '<img alt="The Matrix Poster" title="The Matrix Poster" src="(.*?)" itemprop="image">' 
    pattern = re.compile(regex) 
    #print (str((pattern))) 
    result = re.findall(pattern,htmltext) 
    print result 
    #print type(htmltext) 

回答

0

我认为这个问题是与新线,你可以有(。* \ n *。*?)

+0

感谢,但它仍然给我相同的结果。 – 781850685

+0

你可以尝试正则表达式为'' –

+0

嗨,谢谢。这确实会返回图片的网址,但它是网站上的错误图片。我正在寻找特定代码行内的网址。我可以在它前面添加“The Matrix Poster”吗? – 781850685