爬虫小记

看了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>.*?)&nbsp.*?</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)

心得

回头来看,这个程序还是比较挫,扩展性不好,结构也一般。不过最终看到结果也还是挺兴奋,只有等以后有时间慢慢完善了。先记录下来吧。

最新文章

  1. .NET中使用Redis (二)
  2. 原生js可爱糖果数字时间特效
  3. python进阶笔记 thread 和 threading模块学习
  4. js-我理解的闭包
  5. GLSL Interface Block参考
  6. PLSQL_Oracle PLSQL处理日期方式大全(概念)
  7. [原创]Devexpress XtraReports 系列 8 创建Drill-Through报表
  8. Screwturn搭建企业内部wiki
  9. DNS解析全过程
  10. 网络通信 --&gt; socket通信
  11. [C]排序并插入
  12. #254 Find the Longest Word in a String
  13. 「洛谷2495」「BZOJ3052」「SDOI2001」消耗战【虚树+树形动态规划】
  14. Unity 平台依赖编译
  15. Mac下安装社区版MongoDB
  16. DataGuard 配置须知
  17. 用JS实现判断iframe是否加载完成
  18. JS模拟滚动条(有demo和源码下载,支持拖动 滚轮 点击事件)
  19. Lombok使用详解(转)
  20. RabbitMQ消息队列(五):Routing 消息路由 2[原]

热门文章

  1. RED_HAWK:基于PHP实现的信息收集与SQL注入漏洞扫描工具
  2. CSS3中nth-of-type和nth-last-of-type
  3. linux(二)之linux常用命令一
  4. COGS 862. 二进制数01串【dp+经典二分+字符串】
  5. HDU2063-过山车-匈牙利算法
  6. UVa 12186 树形dp
  7. Flexbox 练习和总结
  8. Git服务搭建及github使用教程
  9. 布隆(Bloom)过滤器 JAVA实现
  10. [20160711][neven代码移植Windows]