爬虫的基本操作


爬虫基础知识

什么是爬虫?

  在最开始,还没有诞生Google和百度等一系列搜索引擎的公司的时候,人们进入一些公司的网站只能通过在浏览器地址栏输入网址的方式访问,如同在很早之前前手机不流行的时候,我们会把各个好友的电话号码抄写在一个电话本上一样将各个公司的网站记录在文档中,很不方便。

  当搜索引擎公司出现的时候,这些搜索引擎公司来做了一个大黄页,把所有网站的网址搜集起来,用户不用和各个公司打交道,而是直接和搜索引擎公司打交道,让搜索引擎帮助自己在它自己制作的大黄页中找出用户需要的内容返回给用户使用。简单点说,就是公司把信息交给搜索引擎公司,让搜索引擎公司在有用户需要本公司信息的时候提交给用户。

  搜索引擎公司搜集数据不光是公司或个人主动上传,主要还会通过网络爬虫爬去网上信息并且提取出关键字更网友搜索。

  整个网络可以理解为通过a标签链接起来的蜘蛛网,可以在网络中获取任何信息。

  爬虫的作用就是搜集网络信息为我所用。

爬虫的分类

  定向爬虫:只爬取某一个或某几个网站,根据自己的需要有专一目的性爬取。

  非定向爬虫:随机爬去整个网络的网站,见什么爬什么。

爬虫爬取大体步骤

 假设我们爬取“汽车之家”的相关数据:https://www.autohome.com.cn/news/

  

下载页面:

  请求网站:https://www.autohome.com.cn/news/  ,返回的为 HTML 页面字符串。

筛选信息:

  使用正则表达式筛选出我们需要的信息。(python非常牛逼的是有大神把正则表达式写好了,都在开源模块里边了。)

开源模块使用的简单案例

  1. # __author : "王佳伟"
  2. # date : 2019-01-16
  3.  
  4. import requests
  5. from bs4 import BeautifulSoup
  6.  
  7. # 1.request 模块
  8. respone = requests.get("https://www.autohome.com.cn/news/")
  9. respone.text
  10.  
  11. # 2.BeautifulSoup 模块
  12. soup = BeautifulSoup(respone.text, features='html.parser') # 把HTML文本转换为对象
  13. target = soup.find(id='auto-channel-lazyload-article')
  14. print(target)

爬取汽车之家数据案例

爬取汽车之家新闻列表的标题即链接

     

  1. # __author : "王佳伟"
  2. # date : 2019-01-16
  3.  
  4. import requests
  5. from bs4 import BeautifulSoup
  6.  
  7. # 1.request 模块
  8.  
  9. # 下载目标网站的HTML源码
  10. response = requests.get(
  11. url="https://www.autohome.com.cn/news/"
  12. )
  13. # 编码查看网页头
  14. # respone.encoding='gb2312'
  15. # 拿到文本并转换为网页自己的编码
  16. response.encoding = response.apparent_encoding
  17. # print(response.text)
  18.  
  19. # 2.BeautifulSoup 模块
  20.  
  21. # 把HTML文本转换为对象,features表示以什么引擎处理 html.parser / lxml
  22. soup = BeautifulSoup(response.text, features='html.parser')
  23. # 根据id属性找,找到对象中 id = auto-channel-lazyload-article 的标签
  24. target = soup.find(id='auto-channel-lazyload-article')
  25. # 打印输出
  26. # print(target)
  27. # 根据标签找 找到所有标签为li的所有标签
  28. li_list = target.find_all('li')
  29. # print(obj)
  30.  
  31. # 循环列表
  32. for i in li_list:
  33. # 找到每个li标签中包含的a标签
  34. # a = i.find('a')
  35. # print(a)
  36. a = i.find('a')
  37. if a:
  38. # print(a.attrs) # 找到a标签的所有属性
  39. print(a.attrs.get('href')) # 找到a标签的href属性
  40. txt = a.find('h3') # 获取a标签中的h3标签
  41. print(txt)
  42. # print(txt.text)

  

爬取下载汽车之家新闻列表的图片

  

  1. # __author : "王佳伟"
  2. # date : 2019-01-16
  3.  
  4. import requests
  5. from bs4 import BeautifulSoup
  6.  
  7. # 1.request 模块
  8.  
  9. # 下载目标网站的HTML源码
  10. response = requests.get(
  11. url="https://www.autohome.com.cn/news/"
  12. )
  13. # 编码查看网页头
  14. # respone.encoding='gb2312'
  15. # 拿到文本并转换为网页自己的编码
  16. response.encoding = response.apparent_encoding
  17. # print(response.text)
  18.  
  19. # 2.BeautifulSoup 模块
  20.  
  21. # 把HTML文本转换为对象,features表示以什么引擎处理 html.parser / lxml
  22. soup = BeautifulSoup(response.text, features='html.parser')
  23. # 根据id属性找,找到对象中 id = auto-channel-lazyload-article 的标签
  24. target = soup.find(id='auto-channel-lazyload-article')
  25. # 打印输出
  26. # print(target)
  27. # 根据标签找 找到所有标签为li的所有标签
  28. li_list = target.find_all('li')
  29. # print(obj)
  30.  
  31. # 循环列表
  32. for i in li_list:
  33. # 找到每个li标签中包含的a标签
  34. # a = i.find('a')
  35. # print(a)
  36. a = i.find('a')
  37. if a:
  38. # print(a.attrs) # 找到a标签的所有属性
  39. # print(a.attrs.get('href')) # 找到a标签的href属性
  40. txt = a.find('h3') # 获取a标签中的h3标签
  41. # print(txt)
  42. print(txt.text)
  43. # 找a标签中的img标签的src属性
  44. img_url = a.find('img').attrs.get('src')
  45. print(img_url)
  46. # 下载图片
  47. img_response = requests.get(url='https:'+img_url)
  48. # 设置文件名
  49. import uuid
  50. # 随机生成文件名
  51. file_name = str(uuid.uuid4()) + '.jpg'
  52. with open(file_name,'wb') as f:
  53. f.write(img_response.content)

  

总结

requests 模块的使用

  1. # 导包
  2. import requests
  3. # 爬取那个网站,填写URL地址
  4. response = requests.get('URL')
  5. # 获取对象的文本内容
  6. response.text
  7. # 获取图片/视频内容
  8. response.content
  9. # 设置编码
  10. response.encoding
  11. # 获取网站自己的编码类型
  12. response.apparent_encoding
  13. # 获取状态码
  14. response.status_code

BeautifulSoup 模块的使用

  1. # 导包
  2. from bs4 import BeautifulSoup
  3.  
  4. # 获取对象
  5. soup = BeautifulSoup('<html>......</html>', features='html.paeser')
  6. # 找到它孩子第一个符合条件的第一个div
  7. v1 = soup.find('div')
  8. # 找它孩子中第一个 id = d1 的标签
  9. v1 = soup.find(id='d1')
  10. # 找到它孩子第一个id=d1的div
  11. v1 = soup.find('div', id='d1')
  12.  
  13. # 用法同find,找所有,返回值为列表
  14. v2 = soup.find_all('div')
  15. v2 = soup.find_all(id='d1')
  16. v2 = soup.find_all('div', id='d1')
  17.  
  18. obj = v1
  19. obj = v2[0]
  20.  
  21. # 获取值
  22. obj.text
  23. # 获取属性
  24. obj.attrs
  25. # 获取属性值
  26. obj.attrs.get('属性')

  

完成!

爬虫的基本操作 requests / BeautifulSoup 的使用的更多相关文章

  1. Python爬虫学习三------requests+BeautifulSoup爬取简单网页

    第一次第一次用MarkDown来写博客,先试试效果吧! 昨天2018俄罗斯世界杯拉开了大幕,作为一个伪球迷,当然也得为世界杯做出一点贡献啦. 于是今天就编写了一个爬虫程序将腾讯新闻下世界杯专题的相关新 ...

  2. 一个超实用的python爬虫功能使用 requests BeautifulSoup

    一个简单的数据爬取的示例 import os,re import requests import random import time from bs4 import BeautifulSoup us ...

  3. 【Python】在Pycharm中安装爬虫库requests , BeautifulSoup , lxml 的解决方法

    BeautifulSoup在学习Python过程中可能需要用到一些爬虫库 例如:requests BeautifulSoup和lxml库 前面的两个库,用Pychram都可以通过 File--> ...

  4. python 爬虫(一) requests+BeautifulSoup 爬取简单网页代码示例

    以前搞偷偷摸摸的事,不对,是搞爬虫都是用urllib,不过真的是很麻烦,下面就使用requests + BeautifulSoup 爬爬简单的网页. 详细介绍都在代码中注释了,大家可以参阅. # -* ...

  5. Python爬虫入门——使用requests爬取python岗位招聘数据

    爬虫目的 使用requests库和BeautifulSoup4库来爬取拉勾网Python相关岗位数据 爬虫工具 使用Requests库发送http请求,然后用BeautifulSoup库解析HTML文 ...

  6. 从0开始学爬虫8使用requests/pymysql和beautifulsoup4爬取维基百科词条链接并存入数据库

    从0开始学爬虫8使用requests和beautifulsoup4爬取维基百科词条链接并存入数据库 Python使用requests和beautifulsoup4爬取维基百科词条链接并存入数据库 参考 ...

  7. 利用requests, beautifulsoup包爬取股票信息网站

    这是第一次用requests, beautifulsoup实现爬虫,此次爬取的是一个股票信息网站:http://www.gupiaozhishi.net.cn. 实现非常简单,只是为了demo使用的数 ...

  8. 【python网络爬虫】之requests相关模块

    python网络爬虫的学习第一步 [python网络爬虫]之0 爬虫与反扒 [python网络爬虫]之一 简单介绍 [python网络爬虫]之二 python uillib库 [python网络爬虫] ...

  9. 猫眼电影爬取(二):requests+beautifulsoup,并将数据存储到mysql数据库

    上一篇通过requests+正则爬取了猫眼电影榜单,这次通过requests+beautifulsoup再爬取一次(其实这个网站更适合使用beautifulsoup库爬取) 1.先分析网页源码 可以看 ...

随机推荐

  1. 利用TensorFlow实现线性回归模型

    准备数据: import numpy as np import tensorflow as tf import matplotlib.pylot as plt # 随机生成1000个点,围绕在y=0. ...

  2. python简单的ftp程序

    服务器端 '''1.读取文件名2.检测文件是否存在3.打开文件4.检测文件大小5.发送文件大小给客户端6.等客户端确认7.开始边读边发数据8.发送md5'''import socket,os,time ...

  3. Sql之left join(左关联)、right join(右关联)、inner join(自关联)的区别

    参考:https://blog.csdn.net/hj7jay/article/details/51749863

  4. webpack配置常用loader加载器

    webapck中使用loader的方法有三种 使用loader之前必须运行安装 : npm install --save-dev xxx-loader (1)通过CLI : 命令行中运行 webpac ...

  5. c#之如何转换文本文件编码格式为utf-8

    如代码: string content = File.ReadAllText(path, Encoding.Default); File.WriteAllText(path, content, Enc ...

  6. Python学习记录之----网络通信(一)

    网络通信 网络通信需要啥? OSI参考模型是啥? socket: 物理层 数据链路层.MAC地址 网络层. IP地址,好了两台机器能找到了,能连上 传输层.发数据,传数据,当然ICMP在网络层,它只是 ...

  7. linux安装部署Nginx

    两个参考地址: NGINX的百度百科:https://baike.baidu.com/item/nginx/3817705?fr=aladdin NGINX的中文网站:http://www.nginx ...

  8. document.createDocumentFragment 以及创建节点速度比较

    document.createDocumentFragment document.createDocumentFragment()方法创建一个新空白的DocumentFragment对象. Docum ...

  9. flask 请求上下文

    一篇引用大牛的 https://www.cnblogs.com/zhaopanpan/p/9457343.html ### 线程安全 ```python# 线程不安全class Foo(object) ...

  10. [转载]web安全之token

    参考:http://blog.csdn.net/sum_rain/article/details/37085771 Token,就是令牌,最大的特点就是随机性,不可预测.一般黑客或软件无法猜测出来. ...