初步运用多进程,利用正则表达式,爬取猫眼电影top100排行榜,储存到本地
来源:互联网 发布:温州p2p软件开发 编辑:程序博客网 时间:2024/06/12 13:54
爬去猫眼top100排行榜电影的相关信息:
# 猫眼电影改版了/20171214
# 在ipython,nootebook为什么无法运行multiprocessing模块,一直显示“*”啊啊啊啊啊!python自带的IDE好使啊,改天用pycharm试一下。/20171214
# pycharm里可以运行,但写入文件时为什么又出现了编码错误的问题……/20171219
# 问题解决了,在juypter nootebook里也能实现了,因为juypter的特性决定的,要将code当文件引入,感谢StackOverflow里的大牛:https://stackoverflow.com/questions/47313732/jupyter-notebook-never-finishes-processing-using-multiprocessing-python-3?answertab=oldest#tab-top /20171220
import reimport requestsfrom requests.exceptions import RequestExceptionimport jsonfrom fake_useragent import UserAgentfrom multiprocessing import Pooldef get_one_page(url): try: """ response = requests.get(url) # 以前的代码没有伪装也行,今天试了下不行了,提示被禁止 """ """ # 用自己复制过来的UA headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'} """ # 新发现的用fake_UA包 headers = {'User-Agent': UserAgent().random} response = requests.get(url,headers = headers) if response.status_code == 200: return response.text return None except RequestException: return Nonedef parse_one_page(html): # re.S:匹配换行符 # 匹配:排名,名字,演员,上映时间,评分 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) # findall以列表形式返回 #print(items) for item in items: # 以迭代器的形式返回字典形式的内容 yield { 'index': item[0], 'image': item[1], 'title': item[2], 'actor': item[3].strip()[3:], # strip()去掉字符串首尾空白符 'time': item[4].strip()[5:], 'score': item[5]+item[6] }def write_to_file(content): # 编码转换 with open('result.txt', 'a', encoding='utf-8') as f: # dumps 将dict转换成str格式 # loads 将str转换成dict格式 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) #parse_one_page(html) for item in parse_one_page(html): print(item) write_to_file(item)if __name__ == '__main__': #main()# for i in range(10): # 爬取多个页面,用for语句来生成翻页内容# main(i*10) pool = Pool() # 开启进程池 pool.map(main,[i*10 for i in range(10)]) pool.close() pool.join()
阅读全文
0 0
- 初步运用多进程,利用正则表达式,爬取猫眼电影top100排行榜,储存到本地
- Requests+正则表达式爬取猫眼电影top100
- 利用requests和正则表达式re爬取猫眼电影top100,并下载图片
- requests+正则表达式爬猫眼电影TOP100
- 猫眼电影TOP100爬取
- 猫眼电影TOP100爬取
- 爬取猫眼电影榜单Top100
- 【爬虫】爬取猫眼电影top100
- Python爬取猫眼电影TOP100
- 爬取猫眼top100电影并存储在本地csv文件中
- python3爬取猫眼电影(电影名称和图片)存到本地使用进程池
- Python爬虫-爬取猫眼电影Top100榜单
- 爬取猫眼top100电影并存储在mysql中
- Python爬虫(三):爬取猫眼电影网经典电影TOP100信息并存入本地Markdown文件(上)
- Python爬虫(三):爬取猫眼电影网经典电影TOP100信息并存入本地Markdown文件(下)
- 爬取猫眼电影专业版实时数据排行榜
- 【3月24日】Requests+正则表达式抓取猫眼电影Top100
- python正则表达式简单爬虫入门+案例(爬取猫眼电影TOP榜)
- XGBoost的以mae作为优化目标探究
- nginx中配置https的服务页面中引用http资源问题
- Android应用界面组件(下)
- Python基础-报错处理try和raise机制
- Advanced Programming in UNIX Environment Episode 19
- 初步运用多进程,利用正则表达式,爬取猫眼电影top100排行榜,储存到本地
- super-linear 和 sub-linear
- PAT乙级 1079. 延迟的回文数 (20)
- iframe 前端 图片预览等
- Android事件处理
- 甲基化特异性区域的计算鉴别
- Web前端开发者必知的9个CSS属性
- linux环境安装mysql
- ZD800环氧富锌底漆