【发布时间】:2015-07-01 22:53:36
【问题描述】:
此代码使用XPath 表达式废弃IMDb 网站(获取电影的标题、年份、排名等)并返回其结果,但其selector() 函数仅正确运行一次然后中断。我该如何解决?
#!/usr/bin/env python3
import lxml.html
import requests as rq
IMDB_HTML = "http://www.imdb.com/filmosearch"
IMDB_JSON = "http://www.imdb.com/xml/find"
class IMDBParser(object):
def __init__(self, role_type=None, sort_type='user_rating, desc',
job_type="actor", title_type="movie"):
self.job_type = job_type
self.sort_type = sort_type
self.title_type = title_type
self.role_type = role_type
self.params = {
'page': 0,
'sort': sort_type,
'role': role_type,
'job_type': job_type,
'title_type': title_type
}
def identity(self):
"""gets actor's name and extracts its id from
imdb website."""
response = rq.get(IMDB_JSON, params={'json': 1, 'nm': 'one',
'q': rq.compat.quote_plus(self.role_type)})
movie_dicts = response.json()
return movie_dicts.get('name_popular', 'name_approx')[0]['id']
def selector(self, expr):
"""gets an expression and extracts all matched then
returns a generator of each matching value."""
self.params['role'] = self.identity()
while True:
self.params['page'] += 1
response = rq.get(IMDB_HTML, params=self.params)
elements = lxml.html.fromstring(response.text).xpath(expr)
if not elements:
break
yield from (element.text for element in elements)
class IMDBApplication(IMDBParser):
def __init__(self, role_type=None, sort_type='user_rating, desc',
job_type="actor", title_type="movie"):
IMDBParser.__init__(self)
self.job_type = job_type
self.sort_type = sort_type
self.title_type = title_type
self.role_type = role_type
def get_titles(self):
"""passes the xpath expression to the function and gets
its return."""
expr = "//*/div/div[2]/div[3]/div/div[2]/h3/a[1]"
return self.selector(expr)
def get_scores(self):
"""passes the xpath expression to the function and gets
its return."""
expr = "//*/div[2]/div[3]/div/div[2]/div/div[1]/strong"
return self.selector(expr)
def get_years(self):
"""passes the xpath expression to the function and gets
its return."""
expr = "//*/div/div[2]/div[3]/div/div[2]/h3/span[2]"
return self.selector(expr)
if __name__ == "__main__":
ia1 = IMDBApplication("Daniel Craig")
print([i for i in ia1.get_titles()])
print([i for i in ia1.get_scores()])
【问题讨论】:
-
它到底是怎么破的?请提供更多细节。
-
@peterh - 代码审查是针对工作代码,而不是调试问题
-
@rolfl 我能理解,为什么。谢谢!
-
现在你已经超过 15 个 repu,所以你可以投票给答案,这是一个很大的奖励。你的昵称真的很有趣:-)
标签: python python-3.x xpath imdb