requests+beautifulsoup4 爬虫实战
来源:互联网 发布:穷女生恋爱知乎 编辑:程序博客网 时间:2024/05/16 02:30
某电影网站手机页面有影视的评分,但不提供排序。为了看高分电影,动手写了爬虫,实现下载影视名称和评分,并输出至文件,后续通过excel处理排序。
#!/usr/bin/python3# -*- coding:utf-8 -*-"""Here is docstring"""# __author__ = c08762import timeimport requestsfrom bs4 import BeautifulSoupnames = []scores = []headers = {'User-Agent':'Mozilla/5.0 (iPhone; U; CPU iPhone OS 5_1_1 like Mac OS X; en) AppleWebKit/534.46.0 (KHTML, like Gecko) CriOS/19.0.1084.60 Mobile/9B206 Safari/7534.48.3'}root_url = 'http://www.dyaihao.com/type/5.html'i = 1print('正在获取 %s' % root_url)resp = requests.get(root_url, headers=headers, timeout=15)while resp.status_code == 200: print('获取一个页面后暂停5秒\n') time.sleep(5) resp.encoding = 'utf-8' soup = BeautifulSoup(resp.text, 'lxml') # type(h3s) is list, 获取电影名 h3s = soup.select('li h3') for h in h3s: # type(t) is str th = h.text names.append(th[3:]) # 获取评分 ps = soup.select('li p') for p in ps: tp = p.text scores.append(tp[:-1]) # 是否有下一页 next_p = soup.find('a', class_="btn btn-primary btn-block") if next_p is None: print('恭喜爬取完毕,正在输出至文本...') name_score = dict(zip(names, scores)) fileObject = open('/home/c08762/sample.txt', 'w') for k, v in name_score.items(): fileObject.write(str(k)) fileObject.write(",") fileObject.write(str(v)) fileObject.write('\n') fileObject.close() print('文本写入完毕!结束') break else: # 如果有进行地址组装,并跳转 build_url = "http://www.dyaihao.com" + next_p['href'] i += 1 if 0 == i % 20: print('\n防反爬,暂停30秒\n') time.sleep(30) print('正在获取 %s' % build_url) resp = requests.get(build_url, headers=headers, timeout=60)else: print('发生页面打开错误')
有待完善:实现每日增量邮件提醒
0 0
- requests+beautifulsoup4 爬虫实战
- 爬虫实战之五--requests详解
- BeautifulSoup4小爬虫
- python3 [爬虫实战] selenium + requests 爬取安居客
- python3 [爬虫实战] selenium + requests 爬取安居客
- Python 爬虫实战(一):使用 requests 和 BeautifulSoup
- 爬虫05 BeautifulSoup4初体验
- python爬虫 BeautifulSoup4官方文档
- 爬虫——使用BeautifulSoup4的爬虫
- python3[爬虫实战] 爬虫之requests爬取新浪微博京东客服
- Python爬虫辅助库BeautifulSoup4用法精要
- python爬虫——beautifulsoup4使用学习
- Python爬虫之正则 & BeautifulSoup4解析HTML
- Python爬虫BeautifulSoup4系列之十
- ubuntu/linux pyhton3.x 安装pip、requests、bs4 BeautifulSoup4
- 爬虫基础----requests库
- 网络爬虫:Requests+lxml
- python requests 小爬虫
- 教你如何迅速秒杀掉:99%的海量数据处理面试题
- CFgym:Database(字符串处理)
- lightoj 1128 倍增法+dp
- 使用Meta,试做网页,3秒后跳转到指定页面
- hdu 3584 Cube(三维树状数组)
- requests+beautifulsoup4 爬虫实战
- 【JVM】JVM系列之垃圾回收(二)
- 内部类
- 硅谷未来科技与创业企业
- Web Service系列之实例之spyne
- 机器学习的最佳入门学习资源
- python犯错记录(长期更新)
- java集合框架(二)——Collection(集合)类
- 【面试】基于二叉树层次遍历相关问题的求解