工欲善其事,必先利其器,我们首先得了解beautifulsoup的使用,这其实是一个比较简单的东西

  BeautifulSoup的基本使用语法规则

  • .find() 使用示例
    soup.find('a')。那么会返回在soup包含的源代码中,遇到的第一个<a>...</a>标签内容对象。
    soup.find('a', id='next')。那么会返回在soup包含的源代码中,遇到的第一个有属性为id,值为next的<a>对象,比如<a id="next">...</a>。(不只可以用id,大部分其他的属性都可以直接使用,比如src、name。 值得注意的是,class这个属性因为是Python关键字,不能直接使用,所以在BS里面,使用class_='...'进行代替 )
    find返回的结果,依然可以继续使用find()或者find_all()方法。如果找不到指定的内容,find会返回None

  • .find_all()使用示例
    soup.find_all('a')。那么会返回在soup包含的源代码中,遇到的所有<a>...</a>标签内容的可迭代对象(我们可以把它看成一个 list 或者数组)
    soup.find_all('a', class_='next')。那么会返回在soup包含的源代码中,遇到的所有属性为class,值为next的<a>的 可迭代对象,比如<a class="next">...</a>。(语法和find也一样,class也不能直接写)
    find_all返回的“list”中的单个对象 依然可以继续使用find()或者find_all()方法。如果找不到指定的内容,find_all会返回一个空的“list”。

  • 获取元素的某个属性
    soup['src],这样我们就能取出soup对象的src属性了。如果该属性不存在,那么程序会报错。

  • 获取元素中的所有文本
    soup.text,假设soup对象为<div>你好<a>复联</a></div>,那么这个操作返回字符串是你好复联

首先我们获得html的源码,然后保存到文件中,使用beautiful读出来解析:

import  requests
from bs4 import BeautifulSoup
url="https://movie.douban.com/cinema/later/chengdu/"
douban_req = requests.get(url)
# print(douban_req.content.decode('utf-8')) #输出获得的内容
#防止被服务器封掉ip,也减轻服务器压力,保存到本地 file_douban = open("douban.html","wb") # 写入文件
file_douban.write(douban_req.content)
file_douban.close()
# 以只读的方式打开文件
file_open=open("douban.html","rb")
html = file_open.read()
file_open.close()
#解析
soup = BeautifulSoup(html,"lxml") # 初始化BeautifulSoup
print(soup.find("link",href="https://img3.doubanio.com/f/shire/52c9997d6d42db58eab418e976a14d5f3eff981e/css/douban.css"))

将所有的电影信息输出

#解析
soup = BeautifulSoup(html,"lxml") # 初始化BeautifulSoup
all_movie=soup.find("div",id="showing-soon",class_="tab-bd") # 获得整个板块
for each_mobie in all_movie.find_all("div",class_="item"):
print(each_mobie)

效果图:

接下来我们对每个具体电影进行切割分析

我们可以看到首先电影的简单信息都在<ul>  </ul>中,因此根据find,和find_all来获得信息

import  requests
from bs4 import BeautifulSoup
url="https://movie.douban.com/cinema/later/chengdu/"
douban_req = requests.get(url)
# print(douban_req.content.decode('utf-8')) #输出获得的内容
#防止被服务器封掉ip,也减轻服务器压力,保存到本地 file_douban = open("douban.html","wb") # 写入文件
file_douban.write(douban_req.content)
file_douban.close()
# 以只读的方式打开文件
file_open=open("douban.html","rb")
html = file_open.read()
file_open.close()
#解析
soup = BeautifulSoup(html,"lxml") # 初始化BeautifulSoup
all_movie=soup.find("div",id="showing-soon",class_="tab-bd") # 获得整个板块
for each_mobie in all_movie.find_all("div",class_="item"):
title=each_mobie.find("a",class_="")#标题名字
ule_title = title["href"]
ul_information = each_mobie.find_all("li",class_="dt")
time =ul_information[0].text
opera = ul_information[1].text
country =ul_information[2].text
people = each_mobie.find("li",class_="dt last").text
trailer= each_mobie.find("a",class_="trailer_icon")
print("电影链接:",title.text )
print(ule_title)
print(time)
print(opera)
print(people)
if trailer is None:
print("暂时没有预告片")
else:
print("预告片:",trailer["href"])
print("")

效果:

自己也可以增加别的元素,如把海报照片保存下来等,其实都是同样的操作。

具体可参考大佬链接:https://www.jianshu.com/p/c64fe2a20bc9

如果数据保存成html或者csv格式:https://www.jianshu.com/p/011abdcee7e4

最新文章

  1. SQLSERVER2008R2数据库的整体导出及单个表的导出步骤
  2. 深入理解CSS溢出overflow &amp; overflow:hidden真的失效了吗[转载]
  3. 计算&amp;IO密集型任务的 优化
  4. POJ1904 King&#39;s Quest(完备匹配可行边:强连通分量)
  5. SQL Server 用户名密码查看
  6. 【转载】取消Debian系统自动锁屏
  7. 资料下载:生活方向盘PPT以及活动录音(2011.02)
  8. Android学习笔记之Json的使用....
  9. 【OpenCV &amp; CUDA】OpenCV和Cuda结合编程
  10. html 表单 dom 注意跟表单的name值一致
  11. Django 的 CSRF 保护机制(转)
  12. CGAffineTransform相关函数
  13. ASP.NET MVC5总结(四)登陆中常用技术解析之验证码
  14. git 401 错误
  15. 从xib加载文件
  16. (跨平台)cocos2d-x C++ or Object-C(前端)调用C# webservices(后台),实现交叉编译到Android/IOS/WinPhone等移动终端设备
  17. 那些年,让我们一起着迷的Spring
  18. 使用java语言编写窗口按钮
  19. cesium Animation显示系统时间
  20. spring的简单入门

热门文章

  1. HDU - 6393 Traffic Network in Numazu (基环树+树链剖分/LCA)
  2. html中表单提交
  3. ThinkPHP 模型方法 setInc() 和 setDec()
  4. spring-data-mongodb两种实现方式对比
  5. PHP入门(五)
  6. 使用oracle Sqlplus中上下键出现乱码的问题
  7. 5.反生成url
  8. Error: Node Sass does not yet support your current environment: Windows 64-bit with Unsupported runtime (64)
  9. Thread的setDaemon(true)方法的作用
  10. jQuery实现表单动态添加与删除数据操作示例