首页 > 解决方案 > 仅获取网站中的一列

问题描述

我怎样才能只得到这个网站的“用户”栏? https://datarecovery.com/rd/default-passwords/

我试着做:

from bs4 import BeautifulSoup
import urllib.request

url = "https://datarecovery.com/rd/default-passwords/"

soup = BeautifulSoup(urllib.request.urlopen(url))
for tag in soup.find_all("span", "paraEight"):
    tag = str(tag)
    print (tag)

但我意识到每列都有“paraEight”类值,所以我得到了每列的所有值。

更新:

soup = BeautifulSoup(urllib.request.urlopen(url))
for tag in soup.select(".table-responsive table tr td:nth-of-type(5) span"):
    tag = str(tag)
    print (tag)

标签: pythonpython-3.xweb-scrapingbeautifulsoup

解决方案


可能你有

  1. 从表中查找所有行
  2. 然后找到标签,在您的情况下,用户位于第 5 位,因此请检查,仅此而已,这是代码示例
    从 bs4 导入 BeautifulSoup
    导入 urllib.request

    url = "https://datarecovery.com/rd/default-passwords/"

    汤 = BeautifulSoup(urllib.request.urlopen(url), 'html.parser')
    table = soup.find('table')
    对于 table.find_all('tr') 中的 tr:
        all_text = []
        ct = 0
        对于 tr 中的 td:
            ct += 1
            文本 = td.get_text(strip=True)
            如果 ct == 5:
                打印(文本)
    #输出为:用户
    # 根
    #技术
    # SNMP写入
    # (没有任何)
    # (没有任何)
    # DOCSIS_APP
    # 行政

推荐阅读