豆瓣爬虫小记(lowB版)
2024-10-14 07:11:14
爬虫小记
看了python正则知识之后,可以利用正则知识学学网络爬虫。
需求分析
按照自己平时浏览的网页,留意下哪个网页的信息对自己有价值,分析要爬取哪些网页信息。这里我先爬取简单的静态网页,豆瓣网经典电影排名及详细信息。
爬取目标信息:排名,电影名,导演,评分,评价人数
具体步骤
设计下载html网页的函数
设计正则规则
存储爬取信息
#!/usr/bin/env python
#coding:utf8
import requests
import re
import json
def download():
response = requests.get(url.format(page=p*25))
res = response.text
return res
def spider_dpuban():
get_html = download()
obj = re.compile('<div class="item">.*?<em class="">(?P<id>.*?)</em>.*?<span class="title">(?P<title>.*?)</span>'
'.*?<p class="">.*?导演:(?P<director>.*?) .*?</p>.*?<span class="rating_num".*?>(?P<num>.*?)</span>.*?<span>(?P<comment>.*?)人评价</span>',
re.S)
res = obj.finditer(get_html)
title = ('排名','电影名','导演','评分','评价人')
with open('douban', 'a', encoding='utf-8') as f:
for i in res:
dic = dict(zip(title, i.group('id', 'title', 'director', 'num', 'comment')))
wr = json.dumps(dic,ensure_ascii=False)
f.write(wr + '\n')
url = 'https://movie.douban.com/top250?start={page}&filter='
for p in range(10):
spider_dpuban()
with open('douban', 'r', encoding='utf-8') as f:
for i in f:
i = json.loads(i)
print(i)
心得
回头来看,这个程序还是比较挫,扩展性不好,结构也一般。不过最终看到结果也还是挺兴奋,只有等以后有时间慢慢完善了。先记录下来吧。
最新文章
- .NET中使用Redis (二)
- 原生js可爱糖果数字时间特效
- python进阶笔记 thread 和 threading模块学习
- js-我理解的闭包
- GLSL Interface Block参考
- PLSQL_Oracle PLSQL处理日期方式大全(概念)
- [原创]Devexpress XtraReports 系列 8 创建Drill-Through报表
- Screwturn搭建企业内部wiki
- DNS解析全过程
- 网络通信 -->; socket通信
- [C]排序并插入
- #254 Find the Longest Word in a String
- 「洛谷2495」「BZOJ3052」「SDOI2001」消耗战【虚树+树形动态规划】
- Unity 平台依赖编译
- Mac下安装社区版MongoDB
- DataGuard 配置须知
- 用JS实现判断iframe是否加载完成
- JS模拟滚动条(有demo和源码下载,支持拖动 滚轮 点击事件)
- Lombok使用详解(转)
- RabbitMQ消息队列(五):Routing 消息路由 2[原]