Python网页爬取最强杀手!

         BeautifulSoup(简称bs4)是一个用于解析HTML和XML文档的Python库,它能够解析HTML页面,从而方便地提取网页数据。

  1. 「安装bs4库」
    在Python环境中,可以通过pip安装命令来安装bs4:

    pip install beautifulsoup4
  2. 「导入bs4」
    在Python脚本中,我们需要先导入bs4库:

    from bs4 import BeautifulSoup
  3. 「解析文档」
    使用bs4解析HTML文档,通常需要一个解析器。常用的解析器有html.parserlxmlhtml5lib。例如,使用html.parser

    soup = BeautifulSoup(html_content, 'html.parser')
  4. 「查找元素」
    bs4提供了多种方法来查找页面元素,如find()find_all()等。可以根据标签名、属性、CSS类等来定位元素。

  5. 「提取数据」
    找到元素后,可以通过.text.get()等属性来提取数据。

常用方法

  1. find():查找文档树中第一个匹配的元素。

    first_heading = soup.find('h1')
  2. find_all():查找文档树中所有匹配的元素。

    paragraphs = soup.find_all('p')
  3. select():使用CSS选择器查找元素。

    links = soup.select('a[href]')
  4. get_text():获取元素的文本内容,并且可以设置参数来控制空白字符的处理。

    text = first_paragraph.get_text()
  5. get():获取元素的属性值。

    href = first_link.get('href')
  6. string():将元素或元素列表转换成一个字符串。

    html = table.string


简单实例

实例1:提取网页标题

from bs4 import BeautifulSoup
import requests

# 获取网页内容
url = 'http://example.com'
response = requests.get(url)
html_content = response.text

# 解析文档
soup = BeautifulSoup(html_content, 'html.parser')

# 提取网页标题
title = soup.find('title').text
print('网页标题:', title)


实例2:解析表格数据

from bs4 import BeautifulSoup
import requests

# 获取网页内容
url = 'http://example.com/table-page'
response = requests.get(url)
html_content = response.text

# 解析文档
soup = BeautifulSoup(html_content, 'html.parser')

# 查找表格
table = soup.find('table')

# 提取表格标题
headers = [header.text for header in table.find_all('th')]

# 提取表格行数据
rows = table.find_all('tr')
for row in rows:
    cols = row.find_all('td')
    cols_data = [ele.text.strip() for ele in cols]
    print('行数据:', cols_data)


实例3:提取链接和图片

from bs4 import BeautifulSoup
import requests

# 获取网页内容
url = 'http://example.com/image-page'
response = requests.get(url)
html_content = response.text

# 解析文档
soup = BeautifulSoup(html_content, 'html.parser')

# 查找所有链接
links = soup.find_all('a')
for link in links:
    href = link.get('href')
    print('链接地址:', href)

# 查找所有图片
images = soup.find_all('img')
for img in images:
    src = img.get('src')
    print('图片地址:', src)


实例4:使用CSS选择器提取数据

from bs4 import BeautifulSoup

# 假设html_content是获取到的HTML内容
soup = BeautifulSoup(html_content, 'html.parser')

# 使用CSS类选择器
items_with_class = soup.select('.my-class')

# 使用CSS属性选择器
items_with_data_attr = soup.select('[data-some-attribute]')

# 使用CSS组合选择器
items_with_specific_parents = soup.select('section > a')


实例5:获取所有标签

from bs4 import BeautifulSoup
import requests
url = 'http://example.com'
response = requests.get(url)
html_content = response.text
soup = BeautifulSoup(html_content, 'html.parser')
#获取所有标签
for tag in soup.find_all(True):
    attrs = tag.attrs
    for attr, value in attrs.items():
        print(f'Tag: {tag.name}, Attr: {attr}, Value: {value}')


原文始发于微信公众号(python学霸):Python网页爬取最强杀手!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/275864.html

(0)
python学霸的头像python学霸bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!