【问题标题】:Python BeautifulSoup HTML parse code selectPython BeautifulSoup HTML 解析代码选择
【发布时间】:2022-01-16 08:05:29
【问题描述】:
@bot.event
async def on_message(message):
         response = requests.get(f"https://steamid.io/lookup/" f"{on_message}")
         steamid = BeautifulSoup(response.text, "html.parser")
         print(steamid.prettify())

当我运行代码时,网站的代码写成html

<table class="table-striped table-hover">
        <tr>
         <td style="width:40%">
          a steamID
         </td>
         <td>
          <code>
           STEAM_0:0:11101
          </code>
         </td>
        </tr>
        <tr>
         <td>
          a steamID3
         </td>
         <td>
          <code>
           [U:1:22202]
          </code>
         </td>
        </tr>
        <tr>
         <td>
          a steamID3 without brackets
         </td>
         <td>
          <code>
           U:1:22202
          </code>
         </td>
        </tr>
        <tr>
         <td>
          a steamID64
         </td>
         <td>
          <code>
           76561197960287930
          </code>
         </td>
        </tr>
        <tr>
         <td>
          a customURL
         </td>
         <td>
          <code>
           gabelogannewell
          </code>
         </td>
        </tr>
        <tr>
         <td>
          a full URL
         </td>
         <td>
          <code>
           http://steamcommunity.com/profiles/76561197960287930
          </code>
         </td>
        </tr>
        <tr>
         <td>
          a full URL with customURL
         </td>
         <td>
          <code>
           http://steamcommunity.com/id/gabelogannewell
          </code>
         </td>
        </tr>
       </table>
       <p>
        <br/>
        Regular names as input may work and will yield a limited list of profiles, but only if the matching customURL is not taken. customURLs always prevail.
       </p>
       <p>
        Trade URLs will
        <i>
         NOT
        </i>
        work.
       </p>
       <p>
        To convert or look up multiple values, use the dropdown arrow to select
        <i>
         extended list
        </i>
        .
       </p>
      </div>
     </section>
    </div>
   </div>

我想从这些代码中解析并选择以下部分

    </tr>
        <tr>
         <td>
          a steamID64
         </td>
         <td>
          <code>
           76561197960287930
          </code>
         </td>
        </tr>

我想从这里解析并选择“76561197960287930”的部分,我该怎么做?

【问题讨论】:

  • 嗨,欢迎来到堆栈溢出 :) 好吧,您要提取的数字在 &lt;code&gt; 节点内。但是,您的 html 中有多个这样的节点,那么是什么让您要选择的部分与其他部分不同(例如,STEAM_0:0:11101)?
  • 我想要 steamID64 所有 steamID64 都以 765611 开头
  • 我明白了。您能否提供一个完整的代码示例,我们可以运行该示例来重现您的输出。
  • 请注意,当您执行实际查找时,生成的页面源是不同的。您展示的是查找值的示例。
  • 你已经尝试了什么?

标签: python html beautifulsoup


【解决方案1】:

您找到一个包含文本 "a steamID64"&lt;tr&gt; 并找到 &lt;code&gt; 标签:

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

print(
    soup.select_one("tr:-soup-contains('a steamID64') code").get_text(strip=True)
)

输出:

76561197960287930z

【讨论】:

  • 它只返回 76561197960287930 而不是 {on_message} 输入值中的 steamID64
  • @TurgutCecen 那么预期的输出究竟是什么?
  • 已修复,感谢您的帮助
  • 你能看看here输出的html已经改变了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-02-06
  • 2014-03-06
  • 2011-07-21
  • 2018-07-10
  • 1970-01-01
  • 2011-06-15
  • 1970-01-01
相关资源
最近更新 更多