python 爬取猫眼下的榜单(一)--单个页面
#!/usr/bin/env python
# -*- coding: utf- -*-
# @Author: Dang Kai
# @Date: -- ::
# @Last Modified time: -- ::
# @E-mail: @qq.com
# @Description: # http://maoyan.com/board/4
# http://maoyan.com/board/4?offset=20 import requests
import re
import json
from requests.exceptions import RequestException def get_one_page(url, headers):
'''获取单页的html'''
try:
reponse = requests.get(url, headers=headers)
if reponse.status_code == :
return reponse.text
else:
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)
# print(items)
for item in items:
yield{
'index': item[],
'image': item[],
'title': item[],
'actor': item[].strip()[:],
'starttime': item[].strip()[:],
'score': item[] + item[]
}
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():
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.75 Safari/537.36'}
html = get_one_page('http://maoyan.com/board/4?', headers)
# print(html)
for item in parse_one_page(html):
print(item)
write_to_file(item)
if __name__ == '__main__':
main()
python 爬取猫眼下的榜单(一)--单个页面的更多相关文章
- 爬虫系列(1)-----python爬取猫眼电影top100榜
对于Python初学者来说,爬虫技能是应该是最好入门,也是最能够有让自己有成就感的,今天在整理代码时,整理了一下之前自己学习爬虫的一些代码,今天先上一个简单的例子,手把手教你入门Python爬虫,爬取 ...
- 使用requests爬取猫眼电影TOP100榜单
Requests是一个很方便的python网络编程库,用官方的话是"非转基因,可以安全食用".里面封装了很多的方法,避免了urllib/urllib2的繁琐. 这一节使用reque ...
- Python爬取猫眼电影100榜并保存到excel表格
首先我们前期要导入的第三方类库有; 通过猫眼电影100榜的源码可以看到很有规律 如: 亦或者是: 根据规律我们可以得到非贪婪的正则表达式 """<div class ...
- python 爬取猫眼电影top100数据
最近有爬虫相关的需求,所以上B站找了个视频(链接在文末)看了一下,做了一个小程序出来,大体上没有修改,只是在最后的存储上,由txt换成了excel. 简要需求:爬虫爬取 猫眼电影TOP100榜单 数据 ...
- 40行代码爬取猫眼电影TOP100榜所有信息
主要内容: 一.基础爬虫框架的三大模块 二.完整代码解析及效果展示 1️⃣ 基础爬虫框架的三大模块 1.HTML下载器:利用requests模块下载HTML网页. 2.HTML解析器:利用re正则表 ...
- 50 行代码教你爬取猫眼电影 TOP100 榜所有信息
对于Python初学者来说,爬虫技能是应该是最好入门,也是最能够有让自己有成就感的,今天,恋习Python的手把手系列,手把手教你入门Python爬虫,爬取猫眼电影TOP100榜信息,将涉及到基础爬虫 ...
- Python 爬取 猫眼 top100 电影例子
一个Python 爬取猫眼top100的小栗子 import json import requests import re from multiprocessing import Pool #//进程 ...
- Python爬虫项目--爬取猫眼电影Top100榜
本次抓取猫眼电影Top100榜所用到的知识点: 1. python requests库 2. 正则表达式 3. csv模块 4. 多进程 正文 目标站点分析 通过对目标站点的分析, 来确定网页结构, ...
- Python 爬取猫眼电影最受期待榜
主要爬取猫眼电影最受期待榜的电影排名.图片链接.名称.主演.上映时间. 思路:1.定义一个获取网页源代码的函数: 2.定义一个解析网页源代码的函数: 3.定义一个将解析的数据保存为本地文件的函数: ...
随机推荐
- 第八章使用java实现面向对象-File I/O
java.io.File类用于表示文件(目录) File类只用于表示文件(目录)的信息(名称.大小等),不能用于文件内容的访问 RandomAccessFile java提供的对文件内容的访问,既可以 ...
- ORA-12541:TNS-12560:ORA-12518:ORA-28040:ORA-01017
说明 环境(参考): Oracle 12c SQL Developer/Navicat Premium(64位)连接数据库 后续出现的错误代码: ORA-12541: no listener TNS- ...
- About custom Theme and Style
For android system, of course you can custom your own style and theme, but you can't break compatibi ...
- What is the relation of theme and it's derived theme.
You know, a theme can derive from other theme in two ways: xx.xxx implicit way and parent="xxx& ...
- UOJ#288:基础数据结构练习题
题面 UOJ Sol 玄学,不会势能分析 所以 维护区间最大最小值 把开根变成区间减法 如果最大值开根后的变化量和最小值的相等,就直接打个减法\(lazy\) # include <bits/s ...
- BZOJ4698: Sdoi2008 Sandy的卡片(二分 hash)
题意 题目链接 Sol 用什么后缀数组啊 直接差分之后 二分+hash找最长公共子串就赢了啊... 时间复杂度:\(O(nlogn)\)(不过我写的是两个log..反正也能过) // luogu-ju ...
- chrome中常用的快捷键
ctrl+n 新建窗口ctrl+shift+n 无痕模式新建窗口ctrl+t 打开新标签页ctrl+shift+t 打开最近关闭的标签页ctrl+tab 标签页之间切换ctrl+w/ctrl+F4 关 ...
- 【转】Javascript异步编程之setTimeout与setInterval
Javascript异步编程之setTimeout与setInterval 转自:http://www.tuicool.com/articles/Ebueua 在谈到异步编程时,本人最主要会从以下三个 ...
- C# CRC16算法实现【转】
/// <summary> /// CRC校验 /// </summary> public class CRC { #region CRC16 public static by ...
- idea导入项目报错:文档中根元素前面的标记必须格式正确
今天从git上面导入项目之后,由于是上周刚刚提交过的,本地也没有什么修改,于是就从gitlab上面直接下载下来了.可是项目启动时候,报错了... 文档中根元素前面的标记必须格式正确 想想 原来是上次提 ...