爬取猫眼电影top100的代码
废话不说,代码附上:
#encoding:utf-8
import requests
import re
import json
from multiprocessing import Pool #多线程模块
#获取网页源代码
def get_one_page(url):
#添加头信息
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 UBrowser/6.2.3964.2 Safari/537.36'}
html = requests.get(url,headers = headers)
if html.status_code == 200:
return html.text
return None
#根据正则表达式提取信息
def parse_one_page(html):
pattern = re.compile('<dd>.*?board-index.*?>(\d+)</i>.*?title="(.*?)".*?data-src="(.*?)".*?star">(.*?)</p>.*?'
+ 'releasetime">(.*?)</p>.*?integer">(.*?)</i>.*?fraction">(.*?)</i>.*?</dd>',re.S)
data = pattern.findall(str(html))
for item in data:
yield{
'top':item[0],
'name':item[1],
'image_src':item[2],
'actor':item[3].strip()[3:],
'releasetime':item[4].strip()[5:],
'score':item[5] + item[6]
}
#将提取得数据写人文件
def write_of_file(content):
# 'a'保留原先的内容,在尾部写入,因为用的是for循环写入,如果用'w'写入只会保留最后一组的数据,或者早打开文件,写入完在关闭,encoding='utf-8'以utf-8编码
with open('result.txt','a',encoding='utf-8') as f:
#ensure_ascii = False使中文正常输出
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)
for item in parse_one_page(html):
print(item)
write_of_file(item)
if __name__ == '__main__':
#定义多线程
pool = Pool()
pool.map(main,[i*10 for i in range(10)])
爬取猫眼电影top100的代码的更多相关文章
- 50 行代码教你爬取猫眼电影 TOP100 榜所有信息
对于Python初学者来说,爬虫技能是应该是最好入门,也是最能够有让自己有成就感的,今天,恋习Python的手把手系列,手把手教你入门Python爬虫,爬取猫眼电影TOP100榜信息,将涉及到基础爬虫 ...
- 40行代码爬取猫眼电影TOP100榜所有信息
主要内容: 一.基础爬虫框架的三大模块 二.完整代码解析及效果展示 1️⃣ 基础爬虫框架的三大模块 1.HTML下载器:利用requests模块下载HTML网页. 2.HTML解析器:利用re正则表 ...
- 爬虫系列(1)-----python爬取猫眼电影top100榜
对于Python初学者来说,爬虫技能是应该是最好入门,也是最能够有让自己有成就感的,今天在整理代码时,整理了一下之前自己学习爬虫的一些代码,今天先上一个简单的例子,手把手教你入门Python爬虫,爬取 ...
- python 爬取猫眼电影top100数据
最近有爬虫相关的需求,所以上B站找了个视频(链接在文末)看了一下,做了一个小程序出来,大体上没有修改,只是在最后的存储上,由txt换成了excel. 简要需求:爬虫爬取 猫眼电影TOP100榜单 数据 ...
- # [爬虫Demo] pyquery+csv爬取猫眼电影top100
目录 [爬虫Demo] pyquery+csv爬取猫眼电影top100 站点分析 代码君 [爬虫Demo] pyquery+csv爬取猫眼电影top100 站点分析 https://maoyan.co ...
- 用requests库爬取猫眼电影Top100
这里需要注意一下,在爬取猫眼电影Top100时,网站设置了反爬虫机制,因此需要在requests库的get方法中添加headers,伪装成浏览器进行爬取 import requests from re ...
- PYTHON 爬虫笔记八:利用Requests+正则表达式爬取猫眼电影top100(实战项目一)
利用Requests+正则表达式爬取猫眼电影top100 目标站点分析 流程框架 爬虫实战 使用requests库获取top100首页: import requests def get_one_pag ...
- # 爬虫连载系列(1)--爬取猫眼电影Top100
前言 学习python有一段时间了,之前一直忙于学习数据分析,耽搁了原本计划的博客更新.趁着这段空闲时间,打算开始更新一个爬虫系列.内容大致包括:使用正则表达式.xpath.BeautifulSoup ...
- Python爬虫项目--爬取猫眼电影Top100榜
本次抓取猫眼电影Top100榜所用到的知识点: 1. python requests库 2. 正则表达式 3. csv模块 4. 多进程 正文 目标站点分析 通过对目标站点的分析, 来确定网页结构, ...
随机推荐
- intellij idea 设置Live Template快速生成自定义代码块
一.设置 类似于宏,话不多少,上步骤 File----->Setting 选择Live Template 新建触发规则 新建触发key 输入模版text 选择在哪个环境触发 选java 如果是其 ...
- 使用HttpWebRequest POST 文件,带参数
public string HttpUploadFile(string url, string file, string paramName, string contentType, NameValu ...
- 基于cookie实现用户验证
#!/usr/bin/env python import tornado.ioloop import tornado.web class IndexHander(tornado.web.Request ...
- 关于handler的再次讨论
主要有两个问题,post方法和sendmessage方法有什么不同? 同一个handler对象发送的message只能发送给自己吗? 问题1: post方法,对于Handler的Post方式来说,它会 ...
- java线程的三种实现方式
线程实现的三种种方式: 一个是继承Thread类,实现run()方法: 一个是实现Runnable接口,实现run()方法: 一个是实现Callable接口,实现call()方法:该方式和实现Runn ...
- 安装了多个php版本,如何编译扩展
cd /data/php-5.5.35/ext/mysqli 找到安装包目录下面的ext目录 ./configure --with-php-config=/usr/local/php5/bin/ph ...
- opencv新版本的数据结构
转自 http://blog.csdn.net/yang_xian521/article/details/7108387 记得我在OpenCV学习笔记(四)——新版本的数据结构core里面讲过新版本的 ...
- Entity Framework Tutorial Basics(12):Model First
Model First development with Entity Framework: In the Model First approach, you create Entities, rel ...
- wordcount小程序
wordcount小程序 (1)github网址 https://github.com/yuyuyu960818/count_txt_file (2)PSP表 PSP2.1 PSP阶段 预估耗时 (分 ...
- Socket编程--并发server
Socket地址复用 int getsockopt(int sockfd, int level, int optname, void *optval, socklen_t *optlen); int ...