Requests+BeautifulSoup+正则表达式爬取猫眼电影Top100(名称,演员,评分,封面,上映时间,简介)
# encoding:utf-8
from requests.exceptions import RequestException
import requests
import re
import json
from multiprocessing import Pool 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):
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[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('MaoyanTop100.txt', 'a', encoding='utf-8') as f:
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__':
pool = Pool()
# for i in range(10):
# main(i*10)
# 加快效率
pool.map(main, [i*10 for i in range(10)])
效果图:

更新(获取封面以及影片简介):
# encoding:utf-8
from requests.exceptions import RequestException
import requests
import json
import re
from urllib import request
from bs4 import BeautifulSoup 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):
pattern = re.compile('<dd>.*?board-index.*?>(\d+)</i>.*?href="(.*?)".*?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[0],
'jump': item[1],
'image': item[2],
'title': item[3],
'actor': item[4].strip()[3:],
'time': item[5].strip()[5:],
'score': item[6]+item[7]
} def parse_summary_page(url):
# url = 'https://maoyan.com/films/1203'
head = {}
# 使用代理
head['User - Agent'] = 'User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.26 Safari/537.36 Core/1.63.6788.400 QQBrowser/10.3.2843.400'
req = request.Request(url, headers=head)
response = request.urlopen(req)
html = response.read()
# 创建request对象
soup = BeautifulSoup(html, 'lxml')
# 找出div中的内容
soup_text = soup.find('span', class_='dra')
# 输出其中的文本
# print(soup_text.text)
return soup_text def write_to_file(content):
with open('newMaoyanTop100.txt', 'a', encoding='utf-8') as f:
f.write(json.dumps(content, ensure_ascii=False)+'\n')
f.close() def main(offset):
url = "http://maoyan.com/board/4?offset="+str(offset*10)
html = get_one_page(url) for item in parse_one_page(html):
# print(item['number'])
# print(item['jump'])
jump_url = "https://maoyan.com"+str(item['jump'])
item['summary'] = str(parse_summary_page(jump_url)).replace("<span class=\"dra\">","").replace("</span>","")
print(item)
write_to_file(item) # 写txt
# for item in parse_one_page(html):
# write_to_file(item['title']) # 爬取100张图片
# path = 'E:\\myCode\\py_test\\MaoyanTop100\\images\\'
# for item in parse_one_page(html):
# urllib.request.urlretrieve(item['image'], '{}{}.jpg'.format(path, item['index'])) if __name__ == '__main__':
for i in range(10):
main(i)
Requests+BeautifulSoup+正则表达式爬取猫眼电影Top100(名称,演员,评分,封面,上映时间,简介)的更多相关文章
- requests和正则表达式爬取猫眼电影Top100练习
1 import requests 2 import re 3 from multiprocessing import Pool 4 from requests.exceptions import R ...
- python3.6 利用requests和正则表达式爬取猫眼电影TOP100
import requests from requests.exceptions import RequestException from multiprocessing import Pool im ...
- PYTHON 爬虫笔记八:利用Requests+正则表达式爬取猫眼电影top100(实战项目一)
利用Requests+正则表达式爬取猫眼电影top100 目标站点分析 流程框架 爬虫实战 使用requests库获取top100首页: import requests def get_one_pag ...
- 爬虫练习之正则表达式爬取猫眼电影Top100
#猫眼电影Top100import requests,re,timedef get_one_page(url): headers={ 'User-Agent':'Mozilla/5.0 (Window ...
- Requests+正则表达式爬取猫眼电影(TOP100榜)
猫眼电影网址:www.maoyan.com 前言:网上一些大神已经对猫眼电影进行过爬取,所用的方法也是各有其优,最终目的是把影片排名.图片.名称.主要演员.上映时间与评分提取出来并保存到文件或者数据库 ...
- Python爬虫实战之Requests+正则表达式爬取猫眼电影Top100
import requests from requests.exceptions import RequestException import re import json # from multip ...
- python爬虫从入门到放弃(九)之 Requests+正则表达式爬取猫眼电影TOP100
import requests from requests.exceptions import RequestException import re import json from multipro ...
- 整理requests和正则表达式爬取猫眼Top100中遇到的问题及解决方案
最近看崔庆才老师的爬虫课程,第一个实战课程是requests和正则表达式爬取猫眼电影Top100榜单.虽然理解崔老师每一步代码的实现过程,但自己敲代码的时候还是遇到了不少问题: 问题1:获取respo ...
- 14-Requests+正则表达式爬取猫眼电影
'''Requests+正则表达式爬取猫眼电影TOP100''''''流程框架:抓去单页内容:利用requests请求目标站点,得到单个网页HTML代码,返回结果.正则表达式分析:根据HTML代码分析 ...
随机推荐
- C++中为何大量使用类指针
C++的精髓之一就是多态性,只有指针或者引用可以达到多态.对象不行类指针的优点: 第一实现多态. 第二,在函数调用,传指针参数.不管你的对象或结构参数多么庞大,你用指针,传过去的就是4个字节.如果用对 ...
- GENIL_BOL_BROWSER 中显示的Object Name 是root object的名字
EMPLOYEE 是root object 的名字. 2: dynamic query parameters 对应于:srch_attr. Each BOL object appears in a t ...
- 如何利用VMware安装XP系统
如何利用VMware安装XP系统 百度经验 http://jingyan.baidu.com/article/215817f78ba0c51eda142322.html 1 运行分区工具 2 ...
- DES、RC4、AES等加密算法优势及应用
[IT168 技术]1篇文章,1部小说被盗取,全靠维(si)权(bi)捍卫自己的原创权利.程序员捍卫自己珍贵的代码,全靠花式的加密算法.代码加密有多重要?程序员半年做出的产品,盗版者可能半天就能完全破 ...
- 【LeetCode每天一题】Longest Palindromic Substring(最长回文字串)
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- [LeetCode] 859. Buddy Strings_Easy
Given two strings A and B of lowercase letters, return true if and only if we can swap two letters i ...
- ntpdate 正确的做法
网上很多搜到的资料都是过时的,主要是时间服务器不能用,不管是国内的还是国外的 1. yum -y install ntp ntpdate 2. 在/etc/crontab中,加入: 0 0 0 * 1 ...
- 判断数组对象里面的某个属性全部为true才执行下一步操作
比如数据[ {name:'张三',isshow:'false'},name:'李四',isshow:'false'}, ] 这里是自己写的验证,没用elemten的 如果有2张票,需要刷2张身份证,则 ...
- 安装Esxi 6.5
最详细安装Esxi 6.5 Exsi 是一款虚拟化系统,与VMware,VirtualBox不同,它不需要安装在其他操作系统上,直接运行在裸机上:占用系统资源很小,易于管理,所以被大多数中小型 ...
- CentOS中利用Docker安装Redis
CentOS中利用Docker安装Redis 1.拉取镜像 #docker pull redis:4.0.10 2.加载镜像 #docker run -p 6379:6379 --name test- ...