Python爬豆瓣电影实例
作者:laozhang 时间:2022-03-22 20:03:12
文件结构
html_downloader.py - 下载网页html内容
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import urllib2
class HtmlDownloader(object):
def downlod(self, url):
if url is None:
return None
response = urllib2.urlopen(url)
if response.getcode() != 200:
return None
return response.read()
html_outputer.py - 输出结果到文件中
#!/usr/bin/python
# -*- coding: UTF-8 -*-
class HtmlOutputer(object):
def collect_data(self, movie_data):
if movie_data is None:
return
fout = open('output.html', 'a+')
for data in movie_data:
print data['name'] + '|', data['rate'] + '|', data['actor'], '\n'
fout.write('%s,' % data['name'].encode('utf-8'))
fout.write('%s,' % data['rate'])
fout.write('%s\n' % data['actor'].encode('utf-8'))
fout.close()
html_parser.py: 解析器:解析html的dom树
#!/usr/bin/python
# -*- coding: UTF-8 -*-
from bs4 import BeautifulSoup
class HtmlParser(object):
def __init__(self):
pass
def parser_html(self, cnt):
if cnt is None:
return
soup = BeautifulSoup(cnt, 'html.parser', from_encoding='utf-8')
# movie_name, movie_desc, movie_rate =
return self.get_movie_names(soup)
def get_movie_names(self, soup):
movie_data = []
movie_all = soup.find('div', class_='article').find_next('table').find_next_sibling('div').find_next_sibling('div').find_all('table')
count = 1
for movie_one in movie_all:
movie_data.append(self.get_movie_name(movie_one))
# if count > 2:
# break
count += 1
return movie_data
def get_movie_name(self, cnt):
info = {}
soup = BeautifulSoup(str(cnt), 'html.parser', from_encoding='utf-8')
movie_one = soup.find('tr', class_='item').find_next('td').find_next_sibling('td').find('div', class_='pl2')
info['name'] = movie_one.find('a').get_text().replace("\n", "").replace(" ", "")
info['actor'] = movie_one.find('p', class_='pl').get_text().replace("\n", "").replace(" ", "")
info['rate'] = movie_one.find('div', class_='star clearfix').find('span', class_='rating_nums').get_text()
return info
spider_main.py - 主函数
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import html_parser, html_outputer, html_downloader
class SpiderMain(object):
def __init__(self):
self.parser = html_parser.HtmlParser()
self.outputer = html_outputer.HtmlOutputer()
self.downloader = html_downloader.HtmlDownloader()
def craw(self, url):
html_cnt = self.downloader.downlod(url)
movie_data = self.parser.parser_html(html_cnt)
self.outputer.collect_data(movie_data)
if __name__ == '__main__':
url = 'https://movie.douban.com/tag/2017?start=100&type=T'
spider = SpiderMain()
spider.craw(url)
综述
其实就是使用了urllib2和BeautifulSoup库,没啥好说的,你也可以直接改url,然后更改html_parser.py文件来满足你自己的爬虫需求。当前也可以更改html_outputer.py来定义保存格式,目前是csv。
标签:Python,豆瓣电影
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python如何实现Excel的最合适列宽(openpyxl)
2023-07-23 04:06:04
![](https://img.aspxhome.com/file/2023/2/95782_0s.png)
Python利用Pillow(PIL)库实现验证码图片的全过程
2022-05-18 21:27:47
![](https://img.aspxhome.com/file/2023/7/81457_0s.png)
10分钟教你用python动画演示深度优先算法搜寻逃出迷宫的路径
2023-10-16 08:05:00
![](https://img.aspxhome.com/file/2023/2/110022_0s.gif)
django实现日志按日期分割
2023-07-20 04:25:21
![](https://img.aspxhome.com/file/2023/5/72885_0s.jpg)
Python实现炸金花游戏的示例代码
2022-01-15 05:24:17
十“问”DreamWeaver
2007-02-03 11:39:00
oracle下加密存储过程的方法
2009-02-28 10:50:00
asp如何显示全部的环境变量?
2010-06-08 09:34:00
Linux环境MySQL服务器级优化讲解
2008-12-04 17:21:00
简单介绍Python中的filter和lambda函数的使用
2023-05-30 18:18:36
利用Python实现图书超期提醒
2021-03-25 18:58:05
![](https://img.aspxhome.com/file/2023/8/131328_0s.png)
基于Python编写一个B站全自动抽奖的小程序
2021-05-03 02:11:07
![](https://img.aspxhome.com/file/2023/8/79788_0s.jpg)
Python argparse中的action=store_true用法小结
2023-07-31 22:35:02
纯CSS图片预加载
2009-10-28 18:40:00
Python imgaug库安装与使用教程(图片加模糊光雨雪雾等特效)
2021-06-23 10:07:16
![](https://img.aspxhome.com/file/2023/5/93695_0s.png)
Python基于datetime或time模块分别获取当前时间戳的方法实例
2021-01-19 22:53:18
Web内容写作:得到更好稿件的头15条规则[译]
2011-06-09 14:38:00
滑动展开/收缩广告代码实例效果
2007-10-09 12:44:00
浅谈python print(xx, flush = True) 全网最清晰的解释
2022-01-28 21:45:48
![](https://img.aspxhome.com/file/2023/7/76737_0s.jpg)
python与xml数据的交互详解
2021-09-12 06:54:10
![](https://img.aspxhome.com/file/2023/1/99681_0s.png)