从零点五开始深度学习

3.爬虫原理及实践

2018-08-02  本文已影响0人  小猪会飞

爬虫原理及实践

什么是爬虫?

爬虫就是用来从网络上获取数据的一种方法工具,当你缺少数据,或者发现有的网站上有你想要的数据的时候,会很有用

什么是html?

html是一种标记语言,示例:

<html><title>标题</title><body>内容</body></html>

实际要遇到的html会相对比较复杂

学习到什么程度?

能够初步掌握爬取简单结构的网页,下载图片

需要前置的知识

html语法,html结构分析,正则表达式,入门时先不讲,但是很重要

# 需要用到的包 requests 讲解
import requests
url = 'http://ring.itools.cn/'
# 请求一个网页,获得其内容
html = requests.get(url)
# 设置字符编码,中文的通常是utf-8 少数的是gbk 出现乱码就换一个试试
html.encoding='utf-8'
# html.text 获取的网页正文内容
print(html.text)
# 加载 bs4包中的BeautifulSoup类,用来解析html结构的包
from bs4 import BeautifulSoup
# 多行文本,可以用六个单引号
text = '''<html><title>这是网页标题</title><body> 
    <div id="content" url="http://www.baidu.com">
        这是主体的内容
    </div>
    <div class="content_ext">
        <h2> 
           <div class="test_class">
            这是附加内容
            <div>
        </h2>
    </div> 
    </body></html> '''
soup = BeautifulSoup(text,"lxml")
# 想要获取内容中的标题,用元素定位
titles = soup.select('title')
# 获取到的是一个列表,读取第一个,要获取元素中间的内容使用.text来获取
print(titles[0].text)
# 想要获取到主体内容,可以通过id定位,id="content"
contents = soup.select('#content')
print(contents[0].text)

# content元素上的url,是元素的属性,获取方式为.get('url')
print(contents[0].get('url'))

# 想要获取到附加内容,可以通过class定位+元素定位 class="content_ext"+下一级的h2元素,中间空格,代表上下级关系
contents_ext = soup.select('.content_ext h2')
print(contents_ext[0].text)
# 要注意到id定位,是#号  class定位用.(英文点)号

爬取http://ring.itools.cn/中的MP3文件

  1. 使用【chrome浏览器】打开http://ring.itools.cn/
  2. 分析页面结构


    step1.jpg
step2.jpg step3.png
# 实战开讲
# 加载request包,http请求常用的包
import requests
# 加载 bs4包中的BeautifulSoup类
from bs4 import BeautifulSoup

# 下载文件函数,第一个参数,文件的网络地址,第二个参数,保存文件地址
def download(url,path):
#     从网络上请求文件
    r = requests.get(url) 
#     将内容写入本地文件
    with open(path, "wb") as wr:
         wr.write(r.content)
# 下载函数结束

# 要爬取的网页地址
url = 'http://ring.itools.cn/'
# 请求页面数据
html = requests.get(url)
# 可以打印出来看看
# print(html.text)

# 开始解析html
soup = BeautifulSoup(html.text,"lxml")
# 这个地方是核心,下面讲,只需要知道,这个地方选中了名称和地址
names = soup.select('.sound h2')
addrs = soup.select('.sound .sound_play')

# 循环遍历名称和地址的列表
for name,addr in zip(names,addrs):
    name = name.text
    addr = addr.get('lurl')
    print('name=',name,'address=',addr)
#     执行下载,将文件下载到 当前文件夹下data/mp3下
    download(addr,'./data/mp3/'+name+'.mp3')

作业,分析https://www.meilele.com/category-chuang/?site_from=cflcd1

你自己先别看下面的示例代码,自己尝试做一下

  1. 获取所有床的名称及价格
  2. 下载所有床的图片放到 ./data/bed/目录下
  3. 在思考一下,现在爬取的都是第一页的内容,如何爬取其他页的内容(提示 https://www.meilele.com/category-chuang/list-p2/?from=page#p
# 加载request包,http请求常用的包
import requests
# 加载 bs4包中的BeautifulSoup类
from bs4 import BeautifulSoup

# 下载文件函数,第一个参数,文件的网络地址,第二个参数,保存文件地址
def download(url,path):
#     从网络上请求文件
    r = requests.get(url) 
#     将内容写入本地文件
    with open(path, "wb") as wr:
         wr.write(r.content)
# 下载函数结束

# 要爬取的网页地址
url = 'https://www.meilele.com/category-chuang/?site_from=cflcd1'
# 请求页面数据
html = requests.get(url)
html.encoding='utf-8'
# print(html.text)

# 开始解析html
soup = BeautifulSoup(html.text,"lxml")
# 这个地方是核心,下面讲,只需要知道,这个地方选中了名称和地址
names = soup.select('.d-name span')
prices = soup.select('.JS_async_price')
imgs = soup.select('.d-img')
index = 0
for name,price,img in zip(names,prices,imgs):
    name = name.text
    price = price.text
    img = img.get('src')
    index = index+1
    print('name=',name,'price=',price,'img=',img)

上一篇 下一篇

猜你喜欢

热点阅读