如何在一个数组中而不是在单独的列表中获取我所有的解析数据?

我试图在页面末尾解析列出字符串方法的表格,但在最后一步被卡住了。我就是这样做的:


from urllib.request import urlopen

from bs4 import BeautifulSoup

url = "https://www.w3schools.com/python/python_strings.asp"

html = urlopen(url)

soup = BeautifulSoup(html, 'lxml')

table = soup.table

rows = table.find_all('tr')

for tr in rows:

  td = tr.find_all('td')

  row = [i.text for i in td]

  print(row)

但这是得到的 O/PI:


屏幕截图:但这是 O/PI 得到的


我怎样才能把它放在一个数组中,这样我就可以很容易地将它变成 df?任何其他建议将不胜感激!


大话西游666
浏览 108回答 2
2回答

萧十郎

当我看到<table>, <tr>,<td>标签时,我会直接使用 pandas(它在引擎盖下使用 BeautifulSoup)。它会为您完成艰苦的工作并对其进行一些清理:import pandas as pdurl = "https://www.w3schools.com/python/python_strings.asp"df = pd.read_html(url)[0]输出:print (df)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Method&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Description0&nbsp; &nbsp; &nbsp;capitalize()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Converts the first character to upper case1&nbsp; &nbsp; &nbsp; &nbsp;casefold()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Converts string into lower case2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;center()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Returns a centered string3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count()&nbsp; Returns the number of times a specified value ...4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;encode()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Returns an encoded version of the string5&nbsp; &nbsp; &nbsp; &nbsp;endswith()&nbsp; Returns true if the string ends with the speci...6&nbsp; &nbsp; &nbsp;expandtabs()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Sets the tab size of the string7&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;find()&nbsp; Searches the string for a specified value and ...8&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;format()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Formats specified values in a string9&nbsp; &nbsp; &nbsp;format_map()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Formats specified values in a string10&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;index()&nbsp; Searches the string for a specified value and ...11&nbsp; &nbsp; &nbsp; &nbsp;isalnum()&nbsp; Returns True if all characters in the string a...12&nbsp; &nbsp; &nbsp; &nbsp;isalpha()&nbsp; Returns True if all characters in the string a...13&nbsp; &nbsp; &nbsp;isdecimal()&nbsp; Returns True if all characters in the string a...14&nbsp; &nbsp; &nbsp; &nbsp;isdigit()&nbsp; Returns True if all characters in the string a...15&nbsp; isidentifier()&nbsp; &nbsp; &nbsp; &nbsp; Returns True if the string is an identifier16&nbsp; &nbsp; &nbsp; &nbsp;islower()&nbsp; Returns True if all characters in the string a...17&nbsp; &nbsp; &nbsp;isnumeric()&nbsp; Returns True if all characters in the string a...18&nbsp; &nbsp;isprintable()&nbsp; Returns True if all characters in the string a...19&nbsp; &nbsp; &nbsp; &nbsp;isspace()&nbsp; Returns True if all characters in the string a...20&nbsp; &nbsp; &nbsp; &nbsp;istitle()&nbsp; Returns True if the string follows the rules o...21&nbsp; &nbsp; &nbsp; &nbsp;isupper()&nbsp; Returns True if all characters in the string a...22&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; join()&nbsp; Joins the elements of an iterable to the end o...23&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ljust()&nbsp; &nbsp; &nbsp;Returns a left justified version of the string24&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;lower()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Converts a string into lower case25&nbsp; &nbsp; &nbsp; &nbsp; lstrip()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Returns a left trim version of the string26&nbsp; &nbsp; &nbsp;maketrans()&nbsp; Returns a translation table to be used in tran...27&nbsp; &nbsp; &nbsp;partition()&nbsp; Returns a tuple where the string is parted int...28&nbsp; &nbsp; &nbsp; &nbsp;replace()&nbsp; Returns a string where a specified value is re...29&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rfind()&nbsp; Searches the string for a specified value and ...30&nbsp; &nbsp; &nbsp; &nbsp; rindex()&nbsp; Searches the string for a specified value and ...31&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rjust()&nbsp; &nbsp; Returns a right justified version of the string32&nbsp; &nbsp; rpartition()&nbsp; Returns a tuple where the string is parted int...33&nbsp; &nbsp; &nbsp; &nbsp; rsplit()&nbsp; Splits the string at the specified separator, ...34&nbsp; &nbsp; &nbsp; &nbsp; rstrip()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Returns a right trim version of the string35&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;split()&nbsp; Splits the string at the specified separator, ...36&nbsp; &nbsp; splitlines()&nbsp; Splits the string at line breaks and returns a...37&nbsp; &nbsp; startswith()&nbsp; Returns true if the string starts with the spe...38&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;strip()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Returns a trimmed version of the string39&nbsp; &nbsp; &nbsp; swapcase()&nbsp; Swaps cases, lower case becomes upper case and...40&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;title()&nbsp; Converts the first character of each word to u...41&nbsp; &nbsp; &nbsp;translate()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Returns a translated string42&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;upper()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Converts a string into upper case43&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;zfill()&nbsp; Fills the string with a specified number of 0 ...

智慧大石

欢迎来到 StackOverflow。希望这可以帮助!对您的 for 循环代码的微小更改>>> data = []>>> len(rows)45>>> for tr in rows:...&nbsp; &nbsp;td = tr.find_all('td')...&nbsp; &nbsp;row = [i.text for i in td]...&nbsp; &nbsp;if row:...&nbsp; &nbsp; &nbsp;data.append(row)创建了一个新变量data来存储您收集的信息。这是一个列表数据类型对象(在 python 中,它类似于数组,但不受大小限制,您可以向其中添加所有类型的数据)。添加了一个if row条件。所以这一行没有找到值,然后那个空列表不会添加到data以下是制作数据框的方法>>> import pandas as pd>>> df = pd.DataFrame(data, columns=['first', 'second'])>>> df.head()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; first&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;second0&nbsp; capitalize()&nbsp; Converts the first \r\n&nbsp; &nbsp; character to upper ...1&nbsp; &nbsp; casefold()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Converts string into \r\n&nbsp; &nbsp; lower case2&nbsp; &nbsp; &nbsp; center()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Returns a centered \r\n&nbsp; &nbsp; string3&nbsp; &nbsp; &nbsp; &nbsp;count()&nbsp; Returns the number of \r\n&nbsp; &nbsp; times a specifie...4&nbsp; &nbsp; &nbsp; encode()&nbsp; &nbsp;Returns an encoded \r\n&nbsp; &nbsp; version of the string
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python