Python学习记录-爬取猫眼电影top100榜单
2018-01-13 00:52
731 查看
安装pycharm-爬取网页内容
在网易云课堂中找到的学习视频,很适合入门级别开发者http://study.163.com/course/courseLearn.htm?courseId=1003735019#/learn/video?lessonId=1004300315&courseId=1003735019
个人总结
1.让控制台显示的内容换行:view->Active Editor 让 Use Soft Wraps 不被勾选2.文件存储:with
open(文件名,读写方式,encoding=“编码格式”)
3.json转字符串:json.dumps(json对象,
ensure_ascii=False)
4.引进requests包
5.str.strip()[3:],截取前三个字符以外的片段
6.进程使用:Pool
7.正则表达式:使用正则表达式,要先将网页内容爬取出来,根据实际爬取的内容写正则式,因为网页中的文件和实际爬取得文件结构可能不一致。
代码如下:
import json import re from multiprocessing.dummy import Pool import requests from requests.exceptions import RequestException def get_one_page(url): try: response = requests.get(url) if response.status_code == 200: return response.text return None except RequestException: return None def parse_one_page(html): pattern = re.compile('<dd>.*?board-index.*?>(\d+)</i>' '.*?data-src="(.*?)"' '.*?name"><a.*?>(.*?)</a>' '.*?star">(.*?)</p>' '.*?releasetime">(.*?)</p>' '.*?integer">(.*?)</i>' '.*?fraction">(.*?)</i>.*?</dd>', re.S) items = re.findall(pattern, html) for item in items: yield { 'index': item[0], 'image': item[1], 'name': item[2], 'actor': item[3], 'time': item[4], 'score': item[5]+item[6] } def write_to_file(content): with open('result.txt', 'a', encoding='utf-8') as f: f.write(json.dumps(content, ensure_ascii=False) + '\n') f.close() def main(offset): url="http://maoyan.com/board/4?offset=" + str(offset) html = get_one_page(url) #print(html) for item in parse_one_page(html): print(item) write_to_file(item) if __name__ == '__main__': #for i in range(10): #main(i*10) pool = Pool() pool.map(main,[i*10 for i in range(10)])
运行结果如下:
相关文章推荐
- Python爬虫-爬取猫眼电影Top100榜单
- Python爬取猫眼电影TOP100
- python3 爬取猫眼榜单top100(requests+beautifulsoup)
- python之爬取猫眼电影TOP100
- python 爬虫抓取猫眼电影 top100 源码
- python正则表达式爬取猫眼电影top100
- python抓取猫眼电影top100
- python爬虫爬取猫眼电影top100
- Python实战---抓取猫眼电影TOP100
- 利用requests和正则爬取猫眼电影top100榜单
- 爬取猫眼电影榜单Top100
- python爬虫实战:抓取猫眼电影TOP100存放到MongoDB中
- Python爬虫之三:抓取猫眼电影TOP100
- Python模块学习:logging 日志记录
- Python学习记录-20160108
- Python学习记录
- Python学习记录1-环境配置
- python学习记录
- Core Python Programming学习记录(1)
- 记录python学习记录