首页 > 其他 > 详细

第二周 网络爬虫之规则

时间:2020-04-14 12:45:06      阅读:70      评论:0      收藏:0      [点我收藏+]

第二周 网络爬虫之规则

本周课程导学

? 内容介绍

单元4: Beautiful Soup库入门

安装

>pip install beautifulsoup4
import requests
from bs4 import BeautifulSoup

r = requests.get(‘https://python123.io/ws/demo.html‘)
demo = r.text

soup = BeautifulSoup(demo,"html.parser")
print(soup)
print(soup.prettify())  # 美化输出


基本元素

Beautiful Soup 库是解析、遍历、维护“标签树”的功能库

基本元素 说明
Tag 最基本的信息组织单元
Name 名字 .name
Attribute 属性 .attrs
NavigableString 标签内非属性字符串 .string
Comment 标签内字符串的注释部分
import requests
from bs4 import BeautifulSoup

r = requests.get(‘https://python123.io/ws/demo.html‘)
demo = r.text

soup = BeautifulSoup(demo,"html.parser")
#print(soup)
print(soup.title)
print(soup.a.name)
print(soup.a.parent.name)
print(soup.a.parent.parent.name)

print(‘--fengexian-----------------‘)


tag  = soup.a
print(tag)
print(tag.attrs)
print(tag.attrs[‘class‘])
print(tag.attrs[‘href‘])
print(type(tag.attrs))
print(type(tag))
print(‘--fengexian-------string----------‘)

print(soup.a.string)
print(soup.p.string)

print(‘--fengexian----------注释-------‘)



D:\develop_study\python\Python38-32\python.exe D:/code_file/first_web/爬虫/慕课/慕课01.py
<title>This is a python demo page</title>
a
p
body
--fengexian-----------------
<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>
{‘href‘: ‘http://www.icourse163.org/course/BIT-268001‘, ‘class‘: [‘py1‘], ‘id‘: ‘link1‘}
[‘py1‘]
http://www.icourse163.org/course/BIT-268001
<class ‘dict‘>
<class ‘bs4.element.Tag‘>
--fengexian-------string----------
Basic Python
The demo python introduces several python courses.
--fengexian----------注释-------

Process finished with exit code 0

基于bs4库的HTML内容遍历方法

下行遍历
属性 说明
.contents 子节点的列表,将所有儿子节点存入列表
.children 子节点的迭代类型,与.contents类似,用于循环遍历儿子节点
.descendants 子孙节点的迭代类型,包含所有子孙节点,用于循环遍历
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 22:45:29) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license()" for more information.

>>> import requests
>>> from bs4 import BeautifulSoup
>>> r = requests.get(‘https://python123.io/ws/demo.html‘)
>>> demo = r.text
>>> soup = BeautifulSoup(demo,"html.parser")
>>> soup.head
<head><title>This is a python demo page</title></head>

>>> soup.head.contents
[<title>This is a python demo page</title>]
>>> soup.body.contents
[‘\n‘, <p class="title"><b>The demo python introduces several python courses.</b></p>, ‘\n‘, <p class="course">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:

<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a> and <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>.</p>, ‘\n‘]
>>> len(soup.body.contents)
5
>>> soup.body.contents[1]
<p class="title"><b>The demo python introduces several python courses.</b></p>
>>> for child in soup.body.children:
	print(child)

	


<p class="title"><b>The demo python introduces several python courses.</b></p>


<p class="course">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:

<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a> and <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>.</p>


>>> 
上行遍历
属性 说明
.parent 节点的父亲标签
.parents 节点先辈标签的迭代类型,用于循环遍历先辈节点
import requests
from bs4 import BeautifulSoup

r = requests.get(‘https://python123.io/ws/demo.html‘)
demo = r.text

soup = BeautifulSoup(demo,"html.parser")

for parent in soup.a.parents:
	if parent is None:
		print(parent)
	else:
		print(parent.name)
p
body
html
[document]

平行遍历
属性 说明
.next_sibling 返回按照HTML文本顺序的下一个平行节点标签
.previous_sibling 返回按照HTML文本顺序的上一个平行节点标签
.next_siblings 迭代类型,返回按照HTML文本顺序的后续所有平行节点标签
.previous_siblings 迭代类型,返回按照HTML文本顺序的前续所有平行节点标签
import requests
from bs4 import BeautifulSoup

r = requests.get(‘https://python123.io/ws/demo.html‘)
demo = r.text

soup = BeautifulSoup(demo,"html.parser")

print(soup.a.next_sibling)
print(soup.a.next_sibling.next_sibling)

print(soup.a.previous_sibling)
print(soup.a.previous_sibling.previous_sibling)


for sibling in soup.a.next_siblings:
    print(sibling)

for sibling in soup.a.previous_siblings:
    print(sibling)

基于bs4库的HTML格式输出

.prettify

import requests
from bs4 import BeautifulSoup

r = requests.get(‘https://python123.io/ws/demo.html‘)
demo = r.text

soup = BeautifulSoup(demo,"html.parser")

print(soup.prettify) 
print("-----------------------------------------------")
print(soup.a.prettify)

单元小结

第二周 网络爬虫之规则

原文:https://www.cnblogs.com/chenfei2928/p/12696812.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!