3.爬虫原理及实践

作者: 小猪会飞 | 来源:发表于2018-08-02 23:35 被阅读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)

相关文章

  • 3.爬虫原理及实践

    爬虫原理及实践 什么是爬虫? 爬虫就是用来从网络上获取数据的一种方法工具,当你缺少数据,或者发现有的网站上有你想要...

  • 1.课程总纲

    课程总纲 课程总纲 基础语法 爬虫原理及实践 Numpy与pandas 数据可视化(暂无内容) python图像学...

  • fastText原理及实践

    1.fastText原理及实践

  • 【Python爬虫】第十一次作业

    一、简单叙述爬虫原理 1.请求数据 2.解析数据 3.获取数据 二、利用chrome浏览器查看某网站的源代码及审查元素

  • 网络爬虫全解析 技术、原理与实践 罗刚 电子工业 2017.3.

    下载地址:网络爬虫全解析 技术、原理与实践 罗刚 电子工业 2017.3[www.rejoiceblog.com]...

  • 爬虫——Web Scraper

    1.认识爬虫 2.利用Excel抓取数据 3.爬虫入门 4.爬虫进阶 5.反爬虫及高阶玩法 6.制作新爬虫步骤 7...

  • 爬虫基础

    我们学习爬虫时候,要先明白这么几件事!1.爬虫是什么?2.爬虫实现爬取的原理是什么?3.爬虫从发起请求到服务器给出...

  • 知识整理术要点【持续行动360天#1】-22/60

    1.新知识和过往旧知识的关联。 2.知识与实践的关联。 3.透过现象发现背后的原理及对应的知识。

  • 《洋葱阅读法》复盘

    1.大脑原理 2.碎片阅读 3.快速阅读 今天看了这仨,明白了爬虫脑(本性),老虎脑(感性),人脑(理性)的原理。...

  • CORS实践及原理

    1.描述. 一开始研究CORS跨域,简直是一头雾水,看的阮老师的文章http://www.ruanyifeng.c...

网友评论

    本文标题:3.爬虫原理及实践

    本文链接:https://www.haomeiwen.com/subject/hgeevftx.html