2 回答

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超13個(gè)贊
當(dāng)我看到<table>, <tr>,<td>標(biāo)簽時(shí),我會(huì)直接使用 pandas(它在引擎蓋下使用 BeautifulSoup)。它會(huì)為您完成艱苦的工作并對(duì)其進(jìn)行一些清理:
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貢獻(xiàn)1946條經(jīng)驗(yàn) 獲得超3個(gè)贊
歡迎來(lái)到 StackOverflow。希望這可以幫助!
對(duì)您的 for 循環(huán)代碼的微小更改
>>> 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)
創(chuàng)建了一個(gè)新變量data來(lái)存儲(chǔ)您收集的信息。這是一個(gè)列表數(shù)據(jù)類(lèi)型對(duì)象(在 python 中,它類(lèi)似于數(shù)組,但不受大小限制,您可以向其中添加所有類(lèi)型的數(shù)據(jù))。
添加了一個(gè)if row條件。所以這一行沒(méi)有找到值,然后那個(gè)空列表不會(huì)添加到data
以下是制作數(shù)據(jù)框的方法
>>> 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
添加回答
舉報(bào)