使用requests库和正则表达式爬取猫眼电影前100

import requests
import re
import json
import time
from requests.exceptions import RequestException def get_one_page(url):
try:
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 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].strip(),
'actor':item[3].strip()[3:] if len(item[3]) > 3 else '',
'time':item[4].strip()[5:] if len(item[4]) > 5 else '',
'score':item[5].strip() + item[6].strip()
} 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 = 'https://maoyan.com/board/4?offset=' + str(offset)
html = get_one_page(url)
# print(html)
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(1)

使用requests库和Beautifulsoup库爬去猫眼电影前100

import requests
from bs4 import BeautifulSoup def gethtmlpage(url):
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36"
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
return response.text
return None def parsehtmlpage(html):
soup = BeautifulSoup(html, 'lxml')
a = soup.select('.movie-item-info a')
return a def write_to_file(content):
with open('result.txt', 'a', encoding="utf-8") as f:
f.writelines(content + '\n') def main(url):
html = gethtmlpage(url)
title = parsehtmlpage(html)
for i in range(0, len(title)):
write_to_file(title[i].string) if __name__ == '__main__':
for i in range(10,100,10):
url = "https://maoyan.com/board/4?offset=%d" % i
main(url)

使用Beautiful库和requests库爬去:

import requests
from bs4 import BeautifulSoup
import bs4 def gethtmlpage(url):
try:
r = requests.get(url)
r.raise_for_status()
r.encoding = r.apparent_encoding
return r.text
except ConnectionError:
return "网络链接出错"
except:
return "未知错误" def parsehtmlpage(html):
soup = BeautifulSoup(html, 'lxml')
ol = soup.select("ol.grid_view")
li = ol[0].select('li')
movie=[]
for i in range(0, len(li)):
index = li[i].select(".pic em")[0].string
title = li[i].find("span", attrs={'class', 'title'}).string
rating_num = li[i].find("span", attrs={'class', 'rating_num'}).string
lianjie = li[i].select(".hd a")[0].get('href')
if isinstance(li[i].find("span", attrs={'class', 'inq'}), bs4.element.Tag):
inq = li[i].find("span", attrs={'class', 'inq'}).string
else:
inq = "没有简介"
movie.append([index, title, rating_num, lianjie, inq])
return movie def writetofile(content):
with open('result.csv', 'a', encoding='utf-8') as f:
f.write(content) def main(url):
html = gethtmlpage(url)
movie = parsehtmlpage(html)
for i in range(0, len(movie)):
writetofile("{0:^5}\t{1:{5}^10}\t{2:^10}\t{3:^40}\t{4:<10}\n".format(movie[i][0], movie[i][1], movie[i][2], movie[i][3], movie[i][4], chr(12288))) if __name__ == '__main__':
writetofile("{0:^5}\t{1:{5}^10}\t{2:^8}\t{3:^40}\t{4:<10}\n".format("排名", "电影名", "评分", "链接", "一句话介绍电影", chr(12288)))
for i in range(0, 10):
url = "https://movie.douban.com/top250?start={}".format(i*25)
main(url)

python应用-爬取猫眼电影top100的更多相关文章

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

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

  2. python 爬取猫眼电影top100数据

    最近有爬虫相关的需求,所以上B站找了个视频(链接在文末)看了一下,做了一个小程序出来,大体上没有修改,只是在最后的存储上,由txt换成了excel. 简要需求:爬虫爬取 猫眼电影TOP100榜单 数据 ...

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

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

  4. 50 行代码教你爬取猫眼电影 TOP100 榜所有信息

    对于Python初学者来说,爬虫技能是应该是最好入门,也是最能够有让自己有成就感的,今天,恋习Python的手把手系列,手把手教你入门Python爬虫,爬取猫眼电影TOP100榜信息,将涉及到基础爬虫 ...

  5. 40行代码爬取猫眼电影TOP100榜所有信息

    主要内容: 一.基础爬虫框架的三大模块 二.完整代码解析及效果展示 1️⃣  基础爬虫框架的三大模块 1.HTML下载器:利用requests模块下载HTML网页. 2.HTML解析器:利用re正则表 ...

  6. # [爬虫Demo] pyquery+csv爬取猫眼电影top100

    目录 [爬虫Demo] pyquery+csv爬取猫眼电影top100 站点分析 代码君 [爬虫Demo] pyquery+csv爬取猫眼电影top100 站点分析 https://maoyan.co ...

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

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

  8. Python爬虫项目--爬取猫眼电影Top100榜

    本次抓取猫眼电影Top100榜所用到的知识点: 1. python requests库 2. 正则表达式 3. csv模块 4. 多进程 正文 目标站点分析 通过对目标站点的分析, 来确定网页结构,  ...

  9. # 爬虫连载系列(1)--爬取猫眼电影Top100

    前言 学习python有一段时间了,之前一直忙于学习数据分析,耽搁了原本计划的博客更新.趁着这段空闲时间,打算开始更新一个爬虫系列.内容大致包括:使用正则表达式.xpath.BeautifulSoup ...

随机推荐

  1. Numpy 数据类型

    numpy支持的数据类型比Python内置的类型多很多,基本上可以和C语言的数据类型对应上, 其中部分类型对应为Python内置的类型.下表列举了常用的Numpy基本类型. 名称 描述 bool_ 布 ...

  2. Python学习——1

    我是一名刚入IT行业的小白,目前主要是做网络运维这一块.曾经总是认为我是做网络运维的,学习代码干啥啊?后来就慢慢发现,传统的运维方式让我的效率好像不如别人效率高,关键还TM看别人比我更轻松.每一个网络 ...

  3. bugku题目“cookie欺骗”

    先上成功截图 题目写的cookie欺骗,但其实是一道考察写脚本能力和代码审计类的题目,首先观察开始的页面 可以看到只有这一串字母,粗略观察可以认为这绝对不是密码,而是胡乱写上的字符,在观察页面源代码后 ...

  4. rn下的弹性布局

    重点: 1]react native 下的弹性布局名字叫:flexDirection 2]flexDirection的默认值是column而不是row,而flex也只能指定一个数字值. 3]使用fle ...

  5. 88、const、static、extern介绍

    一.const与宏的区别 const简介:之前常用的字符串常量,一般是抽成宏,但是苹果不推荐我们抽成宏,推荐我们使用const常量. 执行时刻:宏是预编译(编译之前处理)const是编译阶段. 编译检 ...

  6. Django模板标签

    一.模板标签 1.模板标签是在模板中运用python语言的实现,如for循环,if语句 2.模板标签的运用 2.1在teacher模板下创建students_list模板, 在teacher视图中国创 ...

  7. Codeforces 863 简要题解

    文章目录 A题 B题 C题 D题 E题 F题 G题 传送门 简要题解?因为最后一题太毒不想写了所以其实是部分题解... A题 传送门 题意简述:给你一个数,问你能不能通过加前导000使其成为一个回文数 ...

  8. select、poll、epoll

    1.概念 select.poll.epoll都是事件触发机制,当等待的事件发生就触发进行处理,用于I/O复用 2.简单例子理解 3.select函数 3.1函数详解 int select(int ma ...

  9. Vue下载页面显示内容

    摘抄自 https://www.cnblogs.com/zhangtianqi520/p/9323873.html 先下载所需依赖 npm install --save html2canvas npm ...

  10. REdis zset和double

    平台:x86_64 结论:Zset的最大分数不要超过18014398509481982(17位数字,54位二进制),否则不会得到期望的值. REdis:5.0.4 Zset采用double存储分数值( ...