爬虫笔记2
2023-09-27 14:27:32 时间
Powered by:AB_IN 局外人
BeautifulSoup
import requests
r = requests.get("http://python123.io/ws/demo.html")
from bs4 import BeautifulSoup
soup = BeautifulSoup(r.text, "lxml")
'''
<html><head><title>This is a python demo page</title></head>
<body>
<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>
</body></html>
'''
#soup.tag 返回第一个tag内容
print(soup.title)
print(soup.a)
#标签名
print(soup.a.parent.name) #p 获取a的父亲的名字
print(soup.a.parent.parent.name) #body
tag_a = soup.a
print(tag_a.attrs) #字典 {'href': 'http://www.icourse163.org/course/BIT-268001', 'class': ['py1'], 'id': 'link1'}
print(tag_a.attrs['class']) #['py1']
print(tag_a.string) # 内容 Basic Python
print(type(tag_a.string)) #<class 'bs4.element.NavigableString'>
print(soup.head.contents) #返回儿子结点信息,为列表
#[<title>This is a python demo page</title>]
print(soup.body.contents) #返回儿子结点信息
print(soup.body.contents[1])
print(soup.title.parent) #返回父亲结点信息
#<head><title>This is a python demo page</title></head>
#遍历节点时 if *** is None 不打印
#平行遍历发生在同一个父节点下的各节点间
print(soup.prettify()) #美化的html文档
for link in soup.find_all('a'):
print(link['href'])
#找所有标签为a的链接
完结。
相关文章
- WOS_Crawler: Web of Science图形界面爬虫、解析工具
- 29 爬虫 - re 正则 贪婪模式与非贪婪模式
- Go分布式爬虫笔记(十八) 4月Day2
- Go分布式爬虫笔记(十七) 4月Day1
- Go分布式爬虫笔记(九)
- Python爬虫技术--基础篇--字典和集合
- cURL 学习笔记与总结(2)网页爬虫、天气预报
- Pyton爬虫-流程控制语句
- 模拟用户行为高匿爬虫的设计开发
- java爬虫入门--用jsoup爬取汽车之家的新闻
- 2022年互联网网络爬虫框架TOP10分析
- 基于Python的租房数据分析和房屋租赁爬虫 | 项目案例 | 大学生计算机专业毕业 | 毕业设计
- Python爬虫之Web自动化测试工具Selenium&&Chrome handless
- robots.txt网站爬虫文件设置
- 爬虫相关概念学习笔记
- scrapy爬虫笔记(1):提取首页图片下载链接
- 【网络爬虫】学习笔记:html.parser、lxml、html5lib 三种解析器的区别
- 爬虫的简单介绍