编程语言
首页 > 编程语言> > python爬虫基础(二)---0基础也能看得懂

python爬虫基础(二)---0基础也能看得懂

作者:互联网

python爬虫基础(二)—0基础也能看得懂

前言

BeautifulSoup库
Beautiful Soup是一个可以从HTML或XML文件中提取数据的python库。它能够通过你换的转换器实现惯用的文档导航、查找、修改文档的方式。在python爬虫开发中,我们主要用到的是Beautiful Soup的查找提取功能,修改文档的方式很少用到。

BeautifulSoup(label, ‘html.parser’)传入两个参数,第一个参数表示要提取的对象,第2个参数是html的标志。
常见函数:
prettify函数的作用是将传入的html字符串按照html的格式打印出来
title函数可以查看html页面的标题。
find_all方法可以根据标签名、属性、内容查找文档,定制化查找想要查找的内容。

一、HTML网页内容

HTML: 主体框架,负责文档结构和内容。
CSS: 添加样式(文字大小、颜色、字体加粗等),负责文档样式和布局。
JS: 添加效果,可以让html更加生动好看,负责描述网页的行为。
HTML标记语言的层次非常清晰:
第二层包括头部分head和主体部分body,引入外部文件的标签都可以放在头部,body提供网页的具体内容。

html标签作用(标签往往是成对出现的):

之间是表示文本、描述网页; 之间的文本是可见的网页内容;

之间的文本被显示为标题;

之间的文本被显示为段落。

二、BeautifulSoup库爬取天气

1.常见函数

prettify函数的作用是将传入的html字符串按照html的格式打印出来
title函数可以查看html页面的标题。
find_all方法可以根据标签名、属性、内容查找文档,定制化查找想要查找的内容。

import requests
from bs4 import BeautifulSoup

url = "http://www.baidu.com"
response = requests.get(url)
soup = BeautifulSoup(response.content,"html.parser")

print(soup.prettify())

打印出来的内容如下:

<!DOCTYPE html>
<!--STATUS OK-->
<html>
 <head>
  <meta content="text/html;charset=utf-8" http-equiv="content-type"/>
  <meta content="IE=Edge" http-equiv="X-UA-Compatible"/>
  <meta content="always" name="referrer"/>
  <link href="http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css" rel="stylesheet" type="text/css"/>
  <title>
   百度一下,你就知道
  </title>
 </head>
 <body link="#0000cc">
  <div id="wrapper">
   <div id="head">
    <div class="head_wrapper">
     <div class="s_form">
      <div class="s_form_wrapper">
       <div id="lg">
        <img height="129" hidefocus="true" src="//www.baidu.com/img/bd_logo1.png" width="270"/>
       </div>
       <form action="//www.baidu.com/s" class="fm" id="form" name="f">
        <input name="bdorz_come" type="hidden" value="1"/>
        <input name="ie" type="hidden" value="utf-8"/>
        <input name="f" type="hidden" value="8"/>
        <input name="rsv_bp" type="hidden" value="1"/>
        <input name="rsv_idx" type="hidden" value="1"/>
        <input name="tn" type="hidden" value="baidu"/>
        <span class="bg s_ipt_wr">
         <input autocomplete="off" autofocus="" class="s_ipt" id="kw" maxlength="255" name="wd" value=""/>
        </span>
        <span class="bg s_btn_wr">
         <input class="bg s_btn" id="su" type="submit" value="百度一下"/>
        </span>
       </form>
      </div>
     </div>
     <div id="u1">
      <a class="mnav" href="http://news.baidu.com" name="tj_trnews">
       新闻
      </a>
      <a class="mnav" href="http://www.hao123.com" name="tj_trhao123">
       hao123
      </a>
      <a class="mnav" href="http://map.baidu.com" name="tj_trmap">
       地图
      </a>
      <a class="mnav" href="http://v.baidu.com" name="tj_trvideo">
       视频
      </a>
      <a class="mnav" href="http://tieba.baidu.com" name="tj_trtieba">
       贴吧
      </a>
      <noscript>
       <a class="lb" href="http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1" name="tj_login">
        登录
       </a>
      </noscript>
      <script>
       document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');
      </script>
      <a class="bri" href="//www.baidu.com/more/" name="tj_briicon" style="display: block;">
       更多产品
      </a>
     </div>
    </div>
   </div>
   <div id="ftCon">
    <div id="ftConw">
     <p id="lh">
      <a href="http://home.baidu.com">
       关于百度
      </a>
      <a href="http://ir.baidu.com">
       About Baidu
      </a>
     </p>
     <p id="cp">
      ©2017 Baidu
      <a href="http://www.baidu.com/duty/">
       使用百度前必读
      </a>
      <a class="cp-feedback" href="http://jianyi.baidu.com/">
       意见反馈
      </a>
      京ICP证030173号
      <img src="//www.baidu.com/img/gs.gif"/>
     </p>
    </div>
   </div>
  </div>
 </body>
</html>

在这里插入图片描述
在这里插入图片描述
这里我们要提取图片的链接,可以发现它们是在<img标签里面,src对应的就是链接。
接下来我们写代码,找到相应的图片链接

for link in soup.find_all("img"):
    print(link.get("src"))

在这里插入图片描述

2.爬取天气

import requests
from bs4 import BeautifulSoup

url = "http://www.weather.com.cn/weather1d/101110101.shtml"
response = requests.get(url)
response.encoding = "utf-8"
text = response.text
print(text)

在这里插入图片描述

3.完整代码

在Web前端的CSS语法中,通过CSS也可以定位元素的位置。在写CSS时,标记名不加任何修饰,类名前加点”.“,id名前加#,在这里可以同类似的方法来筛选元素,用到的方法就是soup.select( ),返回类型是list。
在这里插入图片描述
粘贴下来如下:

#today > div.t > ul > li:nth-child(1) > p.tem > span
import requests
from bs4 import BeautifulSoup

url = "http://www.weather.com.cn/weather1d/101190101.shtml"
response = requests.get(url)
response.encoding = "utf-8"
text = response.text
# print(text)
soup = BeautifulSoup(text,"lxml")

weather_info = {"tem":None}
weather = {"daytime":weather_info,"night":weather_info.copy()}

# (数据在html标签中的层级关系)
#today > div.t > ui > li:nth-child(1) > p.tem > span

temp = soup.select("#today > div.t > ul > li:nth-child(1) > p.tem > span")
# print(temp)
# 取出数字
# print(temp[0].string)
weather["daytime"]["tem"] = temp[0].string
#print(weather)
#today > div.t > ul > li:nth-child(2) > p.tem > span
temp = soup.select("#today > div.t > ul > li:nth-child(2) > p.tem > span")
weather["night"]["tem"] = temp[0].string
print(weather)

在这里插入图片描述在这里插入图片描述

总结

本节主要讲解了Beautiful Soup解析HTML解析的各种方式,这也是提取网页数据非常关键的环节。也可以实现简单的爬虫,这些内容配合后面的正则表达式,可以达到事半功倍的效果。

标签:看得懂,tem,python,html,BeautifulSoup,---,weather,print,response
来源: https://blog.csdn.net/suwuzs/article/details/117462721