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
添加回答
举报