最近复习功课,日常码农生活。

import requests
from requests.exceptions import RequestException
import re
import json
from multiprocessing import Pool #requests.get()调用完记得抓异常
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):
#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)
for item in items:
#利用yield生成器
yield{
'index': item[0],
'image': item[1],
'title': item[2],
'actor': item[3].strip()[3:],
'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:
#json.dumps将字典转换为字符串 ,json.load()将json格式数据转换为字典
f.write(json.dumps(content, ensure_ascii=False) + '\n')
f.close() def main(offset):
url = 'https://maoyan.com/board/4?offset=' + str(offset)
html = get_one_page(url)
for item in parse_one_page(html):
print(item)
write_to_file(item) if __name__ == '__main__':
#引入进程池,加快爬取速度
pool = Pool()
pool.map(main, [i*10 for i in range(10)])

requests+正则爬取猫眼电影前100的更多相关文章

  1. PYTHON 爬虫笔记八:利用Requests+正则表达式爬取猫眼电影top100(实战项目一)

    利用Requests+正则表达式爬取猫眼电影top100 目标站点分析 流程框架 爬虫实战 使用requests库获取top100首页: import requests def get_one_pag ...

  2. 用requests库爬取猫眼电影Top100

    这里需要注意一下,在爬取猫眼电影Top100时,网站设置了反爬虫机制,因此需要在requests库的get方法中添加headers,伪装成浏览器进行爬取 import requests from re ...

  3. python学习(23)requests库爬取猫眼电影排行信息

    本文介绍如何结合前面讲解的基本知识,采用requests,正则表达式,cookies结合起来,做一次实战,抓取猫眼电影排名信息. 用requests写一个基本的爬虫 排行信息大致如下图 网址链接为ht ...

  4. Requests+正则表达式爬取猫眼电影

    目标 提取出猫眼电影TOP100的电影名称.时间.评分.图片等信息,提取站点的URL为http://maoyan.com/board/4,提取的结果以文本的形式保存下来. 准备工作 请安装好reque ...

  5. Requests+正则表达式爬取猫眼电影(TOP100榜)

    猫眼电影网址:www.maoyan.com 前言:网上一些大神已经对猫眼电影进行过爬取,所用的方法也是各有其优,最终目的是把影片排名.图片.名称.主要演员.上映时间与评分提取出来并保存到文件或者数据库 ...

  6. Python爬虫学习==>第十章:使用Requests+正则表达式爬取猫眼电影

    学习目的: 通过一个一个简单的爬虫应用,初窥门径. 正式步骤 Step1:流程框架 抓取单页内容:利用requests请求目标站点,得到单个页面的html代码,返回结果: 正则表达式分析:根据html ...

  7. (python3爬虫实战-第一篇)利用requests+正则抓取猫眼电影热映口碑榜

    今天是个值得纪念了日子,我终于在博客园上发表自己的第一篇博文了.作为一名刚刚开始学习python网络爬虫的爱好者,后期本人会定期发布自己学习过程中的经验与心得,希望各位技术大佬批评指正.以下是我自己做 ...

  8. requests库爬取猫眼电影“最受期待榜”榜单 --网络爬虫

    目标站点:https://maoyan.com/board/6 # coding:utf8 import requests, re, json from requests.exceptions imp ...

  9. Requests+正则表达式 爬取猫眼电影

    代码: import re import json from multiprocessing import Pool import requests from requests.exceptions ...

随机推荐

  1. Centos 7 安装composer和Laravel

    composer安装 我安装了lnmp到Centos7里,所以可以直接运行curl -sS https://getcomposer.org/installer | php把安装的composer.ph ...

  2. concat() 方法用于合并两个或多个数组。此方法不会更改现有数组,而是返回一个新数组。

    var arr1 = ['a', 'b', 'c']; var arr2 = ['d', 'e', 'f']; var arr3 = arr1.concat(arr2); // arr3 is a n ...

  3. Leetcode_两数相加_python

    小编从今天起要开始分享一些Leetcode代码,通过好好练习编程能力,争取以后找一份好工作. 题目:两数相加 # Definition for singly-linked list. # class ...

  4. UI测试和GUI测试的区别

    UI 测试 包含GUI测试和command line 测试 分享连接 https://www.ranorex.com/resources/testing-wiki/gui-testing/

  5. ZigZag Conversion 之字形转换字符串

    1.题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows ...

  6. 搭建redis-sentinel(哨兵机制)集群

    redis怎么才能做到高可用 对于redis主从架构,slave可以对应多个本身可以保障高可用,但是对于一个master节点,如果宕机,整个缓存系统就无法进行写的操作,显然整个系统会无法做到高可用 s ...

  7. Shiro 整合 SpringBoot

    https://blog.csdn.net/weixin_38132621/article/details/80216056

  8. python--第二十三天总结(一对多和多对多)

    Django 的 ORM 有多种关系:一对一,多对一,多对多. 各自定义的方式为 :        一对一: OneToOneField        多对一: ForeignKey        多 ...

  9. 【JavaScript】EasyUIのForm的跨域提交问题解析

    昨日.プログラムを作るとき.一つの問題がありますが.皆に共有します. [問題] EasyUIのFormでURLを請求するとき.返却の値が取得できない. [ソース] var fnRegUser = fu ...

  10. 走进MyBatis的世界

    1.MyBatis可框架及ORM 1.Mybatis框架简介 MyBatis是一个开源的数据持久层框架.它内部封装了通过了JDBC访问数据库的操作,支持普通的SQL查询,存储过程和高级映射,几乎消除了 ...