我无法让我的脚本仅打印与 TOOL01 和 TOOL02 匹配的 <TD>

问题描述 投票:0回答:1
import requests
from bs4 import BeautifulSoup
from prettytable import PrettyTable

def find_sort_in_urls(url_list, custom_columns):
    for url in url_list:
        try:
            response = requests.get(url)
            soup = BeautifulSoup(response.text, 'html.parser')
            p = PrettyTable()
            p.field_names = custom_columns

            for table in soup.find_all('table'):
                for row in table.find_all('<tr>'):    
                    cells = row.find_all('td')
                    if any('TOOL01' in cell.get_text() or 'TOOL02' in cell.get_text() for cell in cells):
                        p.add_row([td.get_text() for td in cells])                     
                          
            # Check if the table has any rows added, then print
            if p._rows:
                print(f'{p}\n')

        except requests.exceptions.RequestException as e:
            print(f"Error accessing {url}: {e}")

# List of URLs to check
urls = ["http://path/toolPM.php"]

# Custom column names
custom_columns = ["Location", "Entity", "Entity Type", "Weekly PM"]

find_sort_in_urls(urls, custom_columns)

input("Press ENTER to continue")

我的脚本没有错误,也没有显示任何内容。不知道我的逻辑是否正确

python
1个回答
0
投票

您有

find_all('<tr>')
,但您需要
find_all('tr')
,但没有
< >

这可能都是问题。

© www.soinside.com 2019 - 2024. All rights reserved.