使用urllib + Bs4
#!/usr/bin/env python# _*_ coding:utf-8 _*_import urllib2from bs4 import BeautifulSoupdef agent():# 构造http 请求头headers = {'HOST': '','User-Agent': 'Mozilla/5.0 (Windows NT 6.1; rv:45.0) Gecko/0101 Firefox/45.0','Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8','Accept-Language': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3','Accept-Encoding': 'gzip, deflate, br','DNT': '1','Referer': '/','Connection': 'keep-alive',}return headersdef get_html(url):try:# 发送请求req = urllib2.Request(url)# 使用urllib2获取到页面源码html = urllib2.urlopen(req).read()# print htmlexcept urllib2.HTTPError:return 1else:return htmldef filter_html(url):# 解析html 页面html = get_html(url)# BS不能直接处理url,因此从urllib2那里获取到页面源码,再由BS解析s = BeautifulSoup(html)movie_dict = {}# 获取到页面的div id=screening这个属性的div里面就有当前热门电影,因此我们就获取这整个divfor i in s.find_all('div', id="screening"):# ul标签就是html列表,这个div里面的ul就是电影列表li = i.find_all('ul')# print i.attrsfor j in li:# print j# text是获取列表里面的字符content = j.text.encode('UTF-8')# 获取到的字符里面包含大量的空格因此需要处理name = content.strip().replace(' ', '').split()[0]score = content.strip().replace(' ', '').split()[1]# 将其加入字典电影名称作为键,分数作为值,这样做还有一个原因是获取的结果有重复的话,存到字典里面可以去重movie_dict[name] = score# print '%s 评分:%s' % (name, score)for k, v in movie_dict.items():print k, vif __name__ == "__main__":link = '/'filter_html(link)
github 代码地址
/pythondever/douban