2 回答

TA貢獻1815條經驗 獲得超13個贊
當我看到<table>, <tr>,<td>標簽時,我會直接使用 pandas(它在引擎蓋下使用 BeautifulSoup)。它會為您完成艱苦的工作并對其進行一些清理:
import pandas as pd
url = "https://www.w3schools.com/python/python_strings.asp"
df = pd.read_html(url)[0]
輸出:
print (df)
Method Description
0 capitalize() Converts the first character to upper case
1 casefold() Converts string into lower case
2 center() Returns a centered string
3 count() Returns the number of times a specified value ...
4 encode() Returns an encoded version of the string
5 endswith() Returns true if the string ends with the speci...
6 expandtabs() Sets the tab size of the string
7 find() Searches the string for a specified value and ...
8 format() Formats specified values in a string
9 format_map() Formats specified values in a string
10 index() Searches the string for a specified value and ...
11 isalnum() Returns True if all characters in the string a...
12 isalpha() Returns True if all characters in the string a...
13 isdecimal() Returns True if all characters in the string a...
14 isdigit() Returns True if all characters in the string a...
15 isidentifier() Returns True if the string is an identifier
16 islower() Returns True if all characters in the string a...
17 isnumeric() Returns True if all characters in the string a...
18 isprintable() Returns True if all characters in the string a...
19 isspace() Returns True if all characters in the string a...
20 istitle() Returns True if the string follows the rules o...
21 isupper() Returns True if all characters in the string a...
22 join() Joins the elements of an iterable to the end o...
23 ljust() Returns a left justified version of the string
24 lower() Converts a string into lower case
25 lstrip() Returns a left trim version of the string
26 maketrans() Returns a translation table to be used in tran...
27 partition() Returns a tuple where the string is parted int...
28 replace() Returns a string where a specified value is re...
29 rfind() Searches the string for a specified value and ...
30 rindex() Searches the string for a specified value and ...
31 rjust() Returns a right justified version of the string
32 rpartition() Returns a tuple where the string is parted int...
33 rsplit() Splits the string at the specified separator, ...
34 rstrip() Returns a right trim version of the string
35 split() Splits the string at the specified separator, ...
36 splitlines() Splits the string at line breaks and returns a...
37 startswith() Returns true if the string starts with the spe...
38 strip() Returns a trimmed version of the string
39 swapcase() Swaps cases, lower case becomes upper case and...
40 title() Converts the first character of each word to u...
41 translate() Returns a translated string
42 upper() Converts a string into upper case
43 zfill() Fills the string with a specified number of 0 ...

TA貢獻1946條經驗 獲得超3個贊
歡迎來到 StackOverflow。希望這可以幫助!
對您的 for 循環代碼的微小更改
>>> data = []
>>> len(rows)
45
>>> for tr in rows:
... td = tr.find_all('td')
... row = [i.text for i in td]
... if row:
... data.append(row)
創建了一個新變量data來存儲您收集的信息。這是一個列表數據類型對象(在 python 中,它類似于數組,但不受大小限制,您可以向其中添加所有類型的數據)。
添加了一個if row條件。所以這一行沒有找到值,然后那個空列表不會添加到data
以下是制作數據框的方法
>>> import pandas as pd
>>> df = pd.DataFrame(data, columns=['first', 'second'])
>>> df.head()
first second
0 capitalize() Converts the first \r\n character to upper ...
1 casefold() Converts string into \r\n lower case
2 center() Returns a centered \r\n string
3 count() Returns the number of \r\n times a specifie...
4 encode() Returns an encoded \r\n version of the string
添加回答
舉報