使用request库和正则表达式爬取猫眼电影信息。

1.爬取目标

  猫眼电影TOP100的电影名称,时间,评分,等信息,将结果以文件存储。

2.准备工作

  安装request库。

3.代码实现

 import json
import requests
from requests.exceptions import RequestException
import re
import time def get_one_page(url):
try:
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36'
}
response = requests.get(url, headers=headers)
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],
'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:
f.write(json.dumps(content, ensure_ascii=False) + '\n') def main(offset):
url = 'http://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__':
for i in range(10):
main(offset=i * 10)
time.sleep(2)

爬虫基本库request使用—爬取猫眼电影信息的更多相关文章

  1. Python3爬取猫眼电影信息

    Python3爬取猫眼电影信息 import json import requests from requests.exceptions import RequestException import ...

  2. python爬虫入门新手向实战 - 爬取猫眼电影Top100排行榜

    本次主要爬取Top100电影榜单的电影名.主演和上映时间, 同时保存为excel表个形式, 其他相似榜单也都可以依葫芦画瓢 首先打开要爬取的网址https://maoyan.com/board/4, ...

  3. 利用正则+requests爬取猫眼电影信息

    import json # from multiprocessing import Pool import requests from requests.exceptions import Reque ...

  4. Python爬虫实战之Requests+正则表达式爬取猫眼电影Top100

    import requests from requests.exceptions import RequestException import re import json # from multip ...

  5. 爬虫(七):爬取猫眼电影top100

    一:分析网站 目标站和目标数据目标地址:http://maoyan.com/board/4?offset=20目标数据:目标地址页面的电影列表,包括电影名,电影图片,主演,上映日期以及评分. 二:上代 ...

  6. python应用-爬取猫眼电影top100

    import requests import re import json import time from requests.exceptions import RequestException d ...

  7. 一起学爬虫——使用xpath库爬取猫眼电影国内票房榜

    之前分享了一篇使用requests库爬取豆瓣电影250的文章,今天继续分享使用xpath爬取猫眼电影热播口碑榜 XPATH语法 XPATH(XML Path Language)是一门用于从XML文件中 ...

  8. 爬虫系列(1)-----python爬取猫眼电影top100榜

    对于Python初学者来说,爬虫技能是应该是最好入门,也是最能够有让自己有成就感的,今天在整理代码时,整理了一下之前自己学习爬虫的一些代码,今天先上一个简单的例子,手把手教你入门Python爬虫,爬取 ...

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

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

随机推荐

  1. 创建了以个vagrant box centos php7 nginx swoole git

    php7.2.9 centos7  nginx.1.16  swoole4.4.4 下载地址 链接:https://pan.baidu.com/s/14p7xIa0ZZigRuYvZxnMsYA 提取 ...

  2. 五、SAP中定义变量和给变量赋值

    一.代码如下: 二.执行效果图,如下:

  3. C++ 操作数据库类

    #pragma once #include <string> #include <windows.h> #include <algorithm> #include ...

  4. 百度网盘下载神器 PanDownload v2.0.9(破解版、不限速)

    一直用这个软件来下载百度网盘的东西,不限速,贼爽.  链接:https://pan.baidu.com/s/1UjF47YWd2v9x52c5sjhutQ 提取码:v9pe 也可以直接到官网下载:ht ...

  5. 理解String的intern()方法

    API文档中的介绍: intern public String intern() Returns a canonical representation for the string object. A ...

  6. Runtime之方法交换

    在没有一个类的实现源码的情况下,想改变其中一个方法的实现,除了继承它重写.和借助类别重名方法暴力抢先之外,还有就是方法交换 方法交换的原理:在OC中调用一个方法其实是向一个对象发送消息,查找消息的唯一 ...

  7. 阿里云-容器服务之集群服务 k8s(Jenkins+gitlab+k8s的devops)- 04

    配置jenkins和gitlab: 1.进入jenkins,新增一个项目,demo-piepeline,创建好,点击配置, 2 .设置镜像地址的命名空间: 3.设置镜像的名字 4.设置代码的分支或者t ...

  8. html+css新特性

    audio 音频 viedeo  视频 <audio src = ""02.pogg" controls = "controls"> lo ...

  9. (转载)JVM中的内存模型与垃圾回收

    转载自微信公众号:Java高级架构(Java-jiagou)-----看完这篇文章,我奶奶都知道JVM中的内存模型与垃圾回收了! 六.内存模型 6.1  内存模型与运行时数据区 Java虚拟机在执行J ...

  10. JavaScript—封装animte动画函数

    封装Animte 动画函数 虽然可能以后的开发中可能根本不需要自己写,Jquery 给我们封装好了,或者用CSS3的一些属性达到这样的效果可能更简单. 我比较喜欢底层的算法实现,万变不离其中,这个逻辑 ...