欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

丑憨批的爬虫笔记5信息标记与提取

发布时间:2024/10/8 编程问答 20 豆豆
生活随笔 收集整理的这篇文章主要介绍了 丑憨批的爬虫笔记5信息标记与提取 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

0.信息标记的三种形式


信息标记的作用

HTML:超文本标记

种类:3种
XML


无内容则一个尖括号
Json




Yaml

用缩进表达从属关系,-表示并列关系



1.三种信息标记形式的比较





2.信息提取的一般方法




import requests from bs4 import BeautifulSoup demo =requests.get( 'https://python123.io/ws/demo.html').text soup = BeautifulSoup(demo,'html.parser') ##################### for link in soup.find_all('a'):print(link.get('href') )

3.基于bs4 库的HTML内容查找方法


beautifulsoup库提供了一个方法,查找所有信息

import requests from bs4 import BeautifulSoup demo =requests.get( 'https://python123.io/ws/demo.html').text soup = BeautifulSoup(demo,'html.parser') ##################### soup.find_all('a')#返回一个列表 全是a标签 soup.find_all(['a', 'b']) # 返回一个列表 所有a与b标签 soup.find_all(True)#所有标签 for tag in soup.find_all(True):print(tag.name) #正则表达式库 import re for tag in soup.find_all(re.compile('b')):#以b开头的标签print(tag.name)

import requests from bs4 import BeautifulSoup demo =requests.get( 'https://python123.io/ws/demo.html').text soup = BeautifulSoup(demo,'html.parser') ##################### soup.find_all('p','course')#所有的属性含course的p标签 soup.find_all(attrs='course')#所有得属性含course的标签 soup.find_all(id='link1')#属性‘id’为link1的标签 soup.find_all(id='link') import re soup.find_all(id=re.compile('link'))

soup.find_all(id)# 不能输出 所有含id的标签 而是所有标签
输出含某些属性的所有标签的方法,有点牛哈

import requests from bs4 import BeautifulSoup demo =requests.get( 'https://python123.io/ws/demo.html').text soup = BeautifulSoup(demo,'html.parser') ##################### soup.find_all('a',recursive=False)

import requests from bs4 import BeautifulSoup demo =requests.get( 'https://python123.io/ws/demo.html').text soup = BeautifulSoup(demo,'html.parser') ##################### soup.find_all(string="Basic Python") import re soup.find_all(string=re.compile('Python'))



以下与.find_all()参数相同

summary


因为find_all 太常用了

总结

以上是生活随笔为你收集整理的丑憨批的爬虫笔记5信息标记与提取的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。