亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在一個數組中而不是在單獨的列表中獲取我所有的解析數據?

如何在一個數組中而不是在單獨的列表中獲取我所有的解析數據?

大話西游666 2022-06-22 15:47:57
我試圖在頁面末尾解析列出字符串方法的表格,但在最后一步被卡住了。我就是這樣做的:from urllib.request import urlopenfrom bs4 import BeautifulSoupurl = "https://www.w3schools.com/python/python_strings.asp"html = urlopen(url)soup = BeautifulSoup(html, 'lxml')table = soup.tablerows = 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?任何其他建議將不勝感激!
查看完整描述

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 ...


查看完整回答
反對 回復 2022-06-22
?
智慧大石

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


查看完整回答
反對 回復 2022-06-22
  • 2 回答
  • 0 關注
  • 130 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號