开始学习网络数据挖掘方面的知识,首先从Beautiful Soup入手(Beautiful Soup是一个Python库,功能是从HTML和XML中解析数据),打算以三篇博文纪录学习Beautiful Soup的过程,第一篇是Beautiful Soup基础知识,后两篇利用前边的Beautiful Soup知识完成一个简单的爬虫,抓取allitebook.com的书籍信息和ISBN码,再根据ISBN码去amazon.com抓取书籍对应的价格。
网络数据挖掘指的是从网站中获取数据的过程,数据挖掘技术可以让我们从网站世界中收集大量有价值的数据。
Beautiful Soup是一个Python库,可以从HTML或XML文件中获取数据,利用它你可以做很多事情,比如你可以持续解析某个商品的最新价格,以便跟踪价格的波动情况。
sudo pip3 install beautifulsoup4
from bs4 import BeautifulSoup
html_atag = \"\"\"Test html a tag example
Home
Books
\"\"\"
soup = BeautifulSoup(html_atag, \"html5lib\")
print(soup.a)
html_markup = \"\"\"
-
Carl
32
-
Lucy
25
\"\"\"
在find()方法中传入节点名,例如ul,这样就可以获取第一个匹配的ul节点的内容,例如:
input
student_entries = soup.find(\"ul\")
print(student_entries)
output
-
Carl
32
-
Lucy
25
找到ul节点后,通过观察html可以得知,ul下有2个li,每个li下有2个div,则通过student_entries.li可以获取第一个li节点的数据,继续通过student_entries.li.div可以获取第一个li下第一个div的数据,例如:
input
print(student_entries.li)
output
Carl
32
input
print(student_entries.li.div)
output
Carl
继续通过div.string可以获取div的内容:
input
print(student_entries.li.div.string)
output
\'Carl\'
find()方法支持根据正则表达式查找内容,例如:
input
import re
email_id_example =\"\"\"The below HTML has the information that has email ids.
abc@example.com
xyz@example.com
foo@example.com\"\"\"
soup = BeautifulSoup(email_id_example,\"lxml\")
emailid_regexp = re.compile(\"\\w+@\\w+\\.\\w+\")
first_email_id = soup.find(text=emailid_regexp)
print(first_email_id)
output
abc@example.com
find()方法返回第一个匹配的内容,find_all()方法会返回所有匹配的内容列表,例如上面的根据正则表达式查找邮箱地址,将find()方法换成find_all()方法,则会返回所有匹配成功的内容:
input
all_email_id = soup.find_all(text=emailid_regexp)
print(all_email_id)
output
[\'abc@example.com\', \'xyz@example.com\', \'foo@example.com\']
find_parent()方法往上查找内容,例如,从第一个li节点上使用find_parent()方法,可以获取父节点的内容:
input
print(first_student)
output
Carl
32
input
all_students = first_student.find_parent(\'ul\')
print(all_students)
output
-
Carl
32
-
Lucy
25
sibling是兄弟姐妹的意思,find_next_sibling()方法获取下一个同级别的兄弟节点,例如:
input
second_student = first_student.find_next_sibling()
print(second_student)
output
Lucy
25
其它方法还有很多,例如:
find_next()方法
find_all_next()方法
find_previous_sibling()方法
find_all_previous()方法
这里不在一一赘述,具体请查看官方文档:https://www.crummy.com/software/BeautifulSoup/bs4/doc/#searching-the-tree
使用自节点的标签名即可获取子节点的内容,例如:
input
print(first_student)
output
Carl
32
input
name = first_student.div
print(name)
output
Carl
使用.parent属性可以浏览父节点,例如:
input
print(name.parent)
output
Carl
32
兄弟节点即同级节点,next_sibling和previous_sibling属性分别获取上一个和下一个兄弟节点。例如:
input
print(first_student.next_sibling)
output
Lucy
25
完整方法列表请查看:
https://www.crummy.com/software/BeautifulSoup/bs4/doc/#navigating-the-tree
可以通过.name属性获取某个节点的标签名,同样将某个标签名赋值给.name属性可以很轻易改变标签的名称,例如:
input
first_student
output
Carl
32
input
first_student.name
output
\'li\'
input
first_student.name = \'div\'
first_student.name
output
\'div\'
input
first_student
output
Carl
32
input
first_student[\'class\'] = \'student_new\'
print(first_student)
output
Carl
32
注意:如果class属性没有的话,则此操作不会报错,而变为一个新增操作。
使用del方法可以将一个节点的某个属性删除。例如:
input
del first_student[\'class\']
print(first_student)
output
Carl
32
使用.string属性可以获取标签的内容值(\’Carl\’),同样,对此属性的赋值操作也可以更该其值,例如:
input
print(first_student.div.string)
output
Carl
input
first_student.div.string = \'carl_new\'
print(first_student.div.string)
output
carl_new
使用decompose()方法可以直接删除某个节点:
input
print(first_student)
output
carl_new
32
input
first_student.div.decompose()
print(first_student)
output
32
使用extract()方法同样可以删除某个节点,不过它和decompose()方法不同的是,extract()会返回被删除的这个节点的内容。
我们处于大数据时代,对数据处理感兴趣的朋友欢迎查看另一个系列随笔:利用Python进行数据分析 基础系列随笔汇总
接下来将利用这篇的Beautiful Soup基础知识完成一个简单的爬虫,分别获取两个网站的书籍信息和价格并组合在一起并输出到csv文件中。有兴趣的朋友欢迎关注本博客,也欢迎大家留言进行讨论。