使用BeautifulSoup解析网页

Soup = BeautifulSoup(urlopen(html),'lxml')

Soup为汤,html为食材,lxml为菜谱

from bs4 import BeautifulSoup
from urllib.request import urlopen
Soup = BeautifulSoup(urlopen("http://moumangtai.com/"), "lxml")

描述要爬取的东西在哪

选择要爬取的页面进行检查或按F12可以调出网页的源代码,对要爬取的部分可以选择copy,以当前博客首页大标题为例

copy select:body > header > div > div > div > div > h1

copy Xpath:/html/body/header/div/div/div/div/h1

两者区别在与select多了css样式,但是我们BeautifulSoup只认识copy select ,而Xpath则用于其他库

我的博客为例,来获取大标题和副标题的信息

title = Soup.select("body > header > div > div > div > div > h1")
subtitle = Soup.select("body > header > div > div > div > div > span")
print(title)
print(subtitle)

结果为:

[<h1>QiongDi.W Blog</h1>]
[<span class="subheading">我干了什么 究竟拿了时间换了什么</span>]

再例如每篇文章的标题

copy select:body > div > div > div.col-lg-8.col-lg-offset-1.col-md-8.col-md-offset-1.col-sm-12.col-xs-12.postlist-container > div:nth-child(1) > a > h2

去掉div:nth-child(1)中的筛选后则能爬取相同一类的数据

[<h2 class="post-title">
一日算法
</h2>, <h2 class="post-title">
公共地点人流量计算的云监管平台
</h2>, <h2 class="post-title">
Hello MyBlog
</h2>]

从标签中获得你要的信息

通过调用get_text()即可获取标签内的文本,对于一类数据可以通过for循环获取

for stitle in sontitle:
   print(stitle.get_text())

如果为图片则获取图片的src,即get("src")

对获取到的信息进行整合

假设获取每一篇文章的所有信息

titles = Soup.select("body > div > div > div.col-lg-8.col-lg-offset-1.col-md-8.col-md-offset-1.col-sm-12.col-xs-12.postlist-container > div > a > h2")
subtitles = Soup.select("body > div > div > div.col-lg-8.col-lg-offset-1.col-md-8.col-md-offset-1.col-sm-12.col-xs-12.postlist-container > div > a > h3")
# contents = Soup.select("body > div > div > div.col-lg-8.col-lg-offset-1.col-md-8.col-md-offset-1.col-sm-12.col-xs-12.postlist-container > div > a > div")
messages = Soup.select("body > div > div > div.col-lg-8.col-lg-offset-1.col-md-8.col-md-offset-1.col-sm-12.col-xs-12.postlist-container > div > p")
info = []
for title, subtitle, message in zip(titles, subtitles, messages):
   data = {
       "title": title.get_text().strip(),
       "subtitle": subtitle.get_text().strip(),
       # "content": content.get_text().strip(),
       "message": message.get_text().strip()
  }
   print(data)
   info.append(data)

得到结果:

{'title': '一日算法', 'subtitle': '"Daily algorithm"', 'message': 'Posted by 王琼弟 on April 18, 2019'}
{'title': '公共地点人流量计算的云监管平台', 'subtitle': '"Cloud Monitoring Platform for Human Flow Computing in Public Places"', 'message': 'Posted by 王琼弟 on April 18, 2019'}
{'title': 'Hello MyBlog', 'subtitle': '"Hello World, Hello Blog"', 'message': 'Posted by 王琼弟 on April 17, 2019'}

当一个父节点下有多个子节点而我们需要获取所有的子节点的时候,我们应先爬取他的父节点,然后利用list{父节点.stripped_strings}实现多对一的逻辑获得一个子节点的列表 ps:stripped_strings可以理解为高级的text,可以去除掉所有多余的部分,返回干净的文本信息

筛选信息

for i in list:
   if i["title"]=="Hello MyBlog":
       print(i)
{'title': 'Hello MyBlog', 'subtitle': '"Hello World, Hello Blog"', 'message': 'Posted by 王琼弟 on April 17, 2019'}

The first day of Crawler learning的更多相关文章

  1. The sixth day of Crawler learning

    爬取我爱竞赛网的大量数据 首先获取每一种比赛信息的分类链接 def get_type_url(url):    web_data = requests.get(web_url)    soup = B ...

  2. The fifth day of Crawler learning

    使用mongoDB 下载地址:https://www.mongodb.com/dr/fastdl.mongodb.org/win32/mongodb-win32-x86_64-2008plus-ssl ...

  3. The fourth day of Crawler learning

    爬取58同城 from bs4 import BeautifulSoupimport requestsurl = "https://qd.58.com/diannao/35200617992 ...

  4. The third day of Crawler learning

    连续爬取多页数据 分析每一页url的关联找出联系 例如虎扑 第一页:https://voice.hupu.com/nba/1 第二页:https://voice.hupu.com/nba/2 第三页: ...

  5. The second day of Crawler learning

    用BeatuifulSoup和Requests爬取猫途鹰网 服务器与本地的交换机制 我们每次浏览网页都是再向网页所在的服务器发送一个Request,然后服务器接受到Request后返回Response ...

  6. Machine and Deep Learning with Python

    Machine and Deep Learning with Python Education Tutorials and courses Supervised learning superstiti ...

  7. Node.js Learning Paths

    Node.js Learning Paths Node.js in Action Node.js Expert situations / scenario Restful API OAuth 2.0 ...

  8. 【Machine Learning】KNN算法虹膜图片识别

    K-近邻算法虹膜图片识别实战 作者:白宁超 2017年1月3日18:26:33 摘要:随着机器学习和深度学习的热潮,各种图书层出不穷.然而多数是基础理论知识介绍,缺乏实现的深入理解.本系列文章是作者结 ...

  9. 【Machine Learning】Python开发工具:Anaconda+Sublime

    Python开发工具:Anaconda+Sublime 作者:白宁超 2016年12月23日21:24:51 摘要:随着机器学习和深度学习的热潮,各种图书层出不穷.然而多数是基础理论知识介绍,缺乏实现 ...

随机推荐

  1. POJ-3616_Milking Time

    Milking Time Time Limit: 1000MS Memory Limit: 65536K Description Bessie is such a hard-working cow. ...

  2. 错误处理——According to TLD or attribute directive in tag file, attribute test does not accept any expres

    应用部署运行的时候出现JSP异常, 发生在使用JSTL库的时候: According to TLD or attribute directive in tag file, attribute valu ...

  3. CH1401 兔子与兔子

    #include<bits/stdc++.h> using namespace std; ,p=; typedef unsigned long long ULL;//自然溢出 ULL f[ ...

  4. Project Euler Problem 23-Non-abundant sums

    直接暴力搞就行,优化的地方应该还是计算因子和那里,优化方法在这里:http://www.cnblogs.com/guoyongheng/p/7780345.html 这题真坑,能被写成两个相同盈数之和 ...

  5. git之本地仓库关联远程仓库

    首先新建一个github respository 然后在自己本地新建一个maven项目,里面写点东西 如下图,将自己的项目所在地设置为本地git仓库 将本地仓库与远程关联,首先获取远程仓库的地址,点击 ...

  6. 条件随机场(CRF) - 4 - 学习方法和预测算法(维特比算法)

    声明: 1,本篇为个人对<2012.李航.统计学习方法.pdf>的学习总结,不得用作商用,欢迎转载,但请注明出处(即:本帖地址). 2,由于本人在学习初始时有很多数学知识都已忘记,所以为了 ...

  7. 以P2P网贷为例互联网金融产品如何利用大数据做风控?

    以P2P网贷为例互联网金融产品如何利用大数据做风控?   销售环节 了解客户申请意愿和申请信息的真实性:适用于信贷员模式. 风控关键点 亲见申请人,亲见申请人证件,亲见申请人签字,亲见申请人单位. 审 ...

  8. Codeforces Round #185 (Div. 1 + Div. 2)

    A. Whose sentence is it? 模拟. B. Archer \[pro=\frac{a}{b}+(1-\frac{a}{b})(1-\frac{c}{d})\frac{a}{b}+( ...

  9. ElasticSearch从不懂到会用1—安装篇

    连续加班近一个多月,项目终于告一段落了,也腾出时间写一写项目中用到的东西.在这个项目中,我负责的主要是多种业务场景下的数据查询和搜索,其中搜索用到了ElasticSearch搜索引擎.下面主要围绕El ...

  10. C#将可编译为本地机器码

    微软宣布了.net native的开发者预览版,详见这里. 这是一个大家期待了很多年的特性.每年在技术论坛上都有无数的人问,C#能否编译成本地机器码. 有了这个特性之后,更多开发商会开始选择C#来开发 ...