iT邦幫忙

1

python 如何利用 for 迴圈 印出table內所有 td值?

import requests
from bs4 import BeautifulSoup

payload = {
    '_EVENTTARGET':'',
    '__EVENTARGUMENT':'',
    '__LASTFOCUS':'',
    '__VIEWSTATEGENERATOR': '02BC70F4',
    '__SCROLLPOSITIONX': 0,
    '__SCROLLPOSITIONY': 791.2000122070312,
    '__VIEWSTATE':'/wEPDwUKMTU5MDY1ODkxMw9kFgJmD2QWAgIDD2QWAgIDD2QWCAIDDxBkDxYDZgIBAgIWAxAFA+ihqAUBMGcQBQnntbHoqIjlnJYFATFnEAUN6KGoK+e1seioiOWclgUBMmdkZAIHD2QWHAIBDxBkDxYaZgIBAgICAwIEAgUCBgIHAggCCQIKAgsCDAINAg4CDwIQAhECEgITAhQCFQIWAhcCGAIZFhoQBQQxOTk2BQQxOTk2ZxAFBDE5OTcFBDE5OTdnEAUEMTk5OAUEMTk5OGcQBQQxOTk5BQQxOTk5ZxAFBDIwMDAFBDIwMDBnEAUEMjAwMQUEMjAwMWcQBQQyMDAyBQQyMDAyZxAFBDIwMDMFBDIwMDNnEAUEMjAwNAUEMjAwNGcQBQQyMDA1BQQyMDA1ZxAFBDIwMDYFBDIwMDZnEAUEMjAwNwUEMjAwN2cQBQQyMDA4BQQyMDA4ZxAFBDIwMDkFBDIwMDlnEAUEMjAxMAUEMjAxMGcQBQQyMDExBQQyMDExZxAFBDIwMTIFBDIwMTJnEAUEMjAxMwUEMjAxM2cQBQQyMDE0BQQyMDE0ZxAFBDIwMTUFBDIwMTVnEAUEMjAxNgUEMjAxNmcQBQQyMDE3BQQyMDE3ZxAFBDIwMTgFBDIwMThnEAUEMjAxOQUEMjAxOWcQBQQyMDIwBQQyMDIwZxAFBDIwMjEFBDIwMjFnZGQCAw8QZA8WGmYCAQICAgMCBAIFAgYCBwIIAgkCCgILAgwCDQIOAg8CEAIRAhICEwIUAhUCFgIXAhgCGRYaEAUEMTk5NgUEMTk5NmcQBQQxOTk3BQQxOTk3ZxAFBDE5OTgF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Neo4pa2rfqZ+TijV1KhPZ9t112+wKWgCgUx+90LoKanCA5KPcWQfAgoH6KepdFYaEkToqLP8vQk/5Mt1SgLU+UhCEm5dtO/aqHvneN02hRfoErusn7owDi/6EaUR99VWuBLGMeIbAuquA5IigBbI17Rxp7yuFkFI50Mb+9p3THIrOa9ncmClF8noovKG4awSV383ucXO68AYV4R/NAp0Mf1jseZzrICE+kAHE9RDztg5LWrPrAa0v4eW005XcLM4j/U5VK680L6vSj5qzN4MKPJeqofGOT3sknP5NH0RZEqt5a5rKR0Bz9DwotEEVtmE6nxaUjAgbDxxfUOMDkHeZxX3/MLwlCTvwl8r714CO5OwwWsO0xXcjVkWJGCLkFZo/JPVuewb9aXnZN9w6Xmoa1oyZxp76oo1zVd/0USzB8H7Akxx7SciDog/2wbZyWEALgAvCoBCEsGNw',
    'ctl00$ContentPlaceHolder_content$RadioButtonList_resultType': 0,
    'ctl00$ContentPlaceHolder_content$uc_timeSelector$DropDownList_startYear': 2021,
    'ctl00$ContentPlaceHolder_content$uc_timeSelector$DropDownList_endYear': 2021,
    'ctl00$ContentPlaceHolder_content$uc_timeSelector$time':'RadioButton_month',
    'ctl00$ContentPlaceHolder_content$uc_timeSelector$DropDownList_month_startYear': 2018,
    'ctl00$ContentPlaceHolder_content$uc_timeSelector$DropDownList_month_startMonth': 1,
    'ctl00$ContentPlaceHolder_content$uc_timeSelector$DropDownList_month_endYear': 2021,
    'ctl00$ContentPlaceHolder_content$uc_timeSelector$DropDownList_month_endMonth': 5,
    'ctl00$ContentPlaceHolder_content$uc_productList_mixVer$CheckBoxList_priceType$0': 1,
    'ctl00$ContentPlaceHolder_content$uc_productList_mixVer$CheckBoxList_productList_1_0$8': 13,
    'ctl00$ContentPlaceHolder_content$Button_query':'查詢'
}

url = 'http://price.naif.org.tw/Query/Query_now.aspx'

headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'}

resp = requests.post(url, data = payload)


# 將 HTML 轉成 BeautifulSoup 物件
soup = BeautifulSoup(resp.text, 'html.parser')

table = soup.find_all("table", {"id":"ContentPlaceHolder_content_GridView_data"})

print(table[0])

我利用上方程式碼印出

<table align="Center" border="1" cellspacing="0" class="queryBoxNew" id="ContentPlaceHolder_content_GridView_data" rules="all" width="100%">
<tr>
<th rowspan="3"><div style="max-width:140px;min-width:100%;">時間</div></th><th colspan="1">產地農場價格</th>
</tr><tr>
<th colspan="1">家禽類</th>
</tr><tr>
<th><div style="max-width:120px;min-width:100%;">雞蛋</div></th>
</tr>**<tr>
<td>2018年1月</td><td>45.35</td>
</tr><tr>
<td>2018年2月</td><td>38.63</td>
</tr><tr>
<td>2018年3月</td><td>38.58</td>
</tr><tr>
<td>2018年4月</td><td>43.06</td>
</tr><tr>
<td>2018年5月</td><td>46.32</td>
</tr><tr>
<td>2018年6月</td><td>44.33</td>
</tr><tr>
<td>2018年7月</td><td>47.34</td>
</tr><tr>
<td>2018年8月</td><td>51.75</td>
</tr><tr>
<td>2018年9月</td><td>55.39</td>
</tr><tr>
<td>2018年10月</td><td>55.83</td>
</tr><tr>
<td>2018年11月</td><td>55.83</td>
</tr><tr>
<td>2018年12月</td><td>55.83</td>
</tr><tr>
<td>2019年1月</td><td>57.93</td>
</tr><tr>
<td>2019年2月</td><td>57.26</td>
</tr><tr>
<td>2019年3月</td><td>59.87</td>
</tr><tr>
<td>2019年4月</td><td>50.78</td>
</tr><tr>
<td>2019年5月</td><td>44.17</td>
</tr><tr>
<td>2019年6月</td><td>38.94</td>
</tr><tr>
<td>2019年7月</td><td>37.39</td>
</tr><tr>
<td>2019年8月</td><td>45.19</td>
</tr><tr>
<td>2019年9月</td><td>45.50</td>
</tr><tr>
<td>2019年10月</td><td>42.18</td>
</tr><tr>
<td>2019年11月</td><td>46.17</td>
</tr><tr>
<td>2019年12月</td><td>46.91</td>
</tr><tr>
<td>2020年1月</td><td>39.22</td>
</tr><tr>
<td>2020年2月</td><td>42.96</td>
</tr><tr>
<td>2020年3月</td><td>48.84</td>
</tr><tr>
<td>2020年4月</td><td>49.17</td>
</tr><tr>
<td>2020年5月</td><td>44.92</td>
</tr><tr>
<td>2020年6月</td><td>40.83</td>
</tr><tr>
<td>2020年7月</td><td>38.47</td>
</tr><tr>
<td>2020年8月</td><td>45.13</td>
</tr><tr>
<td>2020年9月</td><td>46.78</td>
</tr><tr>
<td>2020年10月</td><td>41.05</td>
</tr><tr>
<td>2020年11月</td><td>43.72</td>
</tr><tr>
<td>2020年12月</td><td>43.84</td>
</tr><tr>
<td>2021年1月</td><td>45.67</td>
</tr><tr>
<td>2021年2月</td><td>42.86</td>
</tr><tr>
<td>2021年3月</td><td>51.48</td>
</tr><tr>
<td>2021年4月</td><td>52.94</td>
</tr><tr>
<td>2021年5月</td><td>46.69</td>
</tr>**
</table>

我想要將標粗那些td數值print出來
像這樣呈現

d = {'時間': ['2021年1月', '2021年2月'], '雞蛋產地價格(元/公斤)': [45.67, 42.86]}
df = pd.DataFrame(data = d)
df
      時間	 雞蛋產地價格(元/公斤)
0	2021年1月	       45.67
1	2021年2月	       42.86

我想利用for迴圈來撰寫
我找到這段程式碼可以print下一筆td資料

egg = soup.find("td", text = "2018年1月").find_next_sibling("td").text

可是他的text內要直接打該值

i = soup.find("td", text = egg).find_next_sibling("td").text

若將text = egg,但egg是變數,這方法會失敗,

有什麼方法可以一直print出td的值,
謝謝!

可以考慮搜尋tr中 第二筆td值
tr td:nth-of-type(2)
cgc0800 iT邦新手 5 級 ‧ 2021-07-08 15:51:28 檢舉
找到解法:
tr = soup.find_all('tr')
for trr in tr:
tdlist = trr.find_all('td')
for i in range(len(tdlist)):
print(str(tdlist[i].text))

2 個回答

1
japhenchen
iT邦大師 1 級 ‧ 2021-07-08 16:41:12
最佳解答

使用pandas跟numpy

import requests
from bs4 import BeautifulSoup
import pandas as pd
import numpy as np
# ..........中間一大堆都一樣

# 最後幾行 
soup = BeautifulSoup(resp.text, 'html.parser')
table = soup.find_all("table", {"id":"ContentPlaceHolder_content_GridView_data"})
#上面這幾行還是你自己寫的


#底下才是重點,也是你下一步該學的,DataFrame
table_MN = pd.read_html(str(table[0]))
df = table_MN[0]
for i in df.to_numpy():
    print(f"時間{i[0]}\t價格{i[1]}")

輸出結果(加\t TAB好看一點)

時間2018年1月   價格45.35
時間2018年2月   價格38.63
時間2018年3月   價格38.58
時間2018年4月   價格43.06
時間2018年5月   價格46.32
時間2018年6月   價格44.33
時間2018年7月   價格47.34
時間2018年8月   價格51.75
時間2018年9月   價格55.39
時間2018年10月  價格55.83
時間2018年11月  價格55.83
時間2018年12月  價格55.83
時間2019年1月   價格57.93
時間2019年2月   價格57.26
時間2019年3月   價格59.87
時間2019年4月   價格50.78
時間2019年5月   價格44.17
時間2019年6月   價格38.94
時間2019年7月   價格37.39
時間2019年8月   價格45.19
時間2019年9月   價格45.5
時間2019年10月  價格42.18
時間2019年11月  價格46.17
時間2019年12月  價格46.91
時間2020年1月   價格39.22
時間2020年2月   價格42.96
時間2020年3月   價格48.84
時間2020年4月   價格49.17
時間2020年5月   價格44.92
時間2020年6月   價格40.83
時間2020年7月   價格38.47
時間2020年8月   價格45.13
時間2020年9月   價格46.78
時間2020年10月  價格41.05
時間2020年11月  價格43.72
時間2020年12月  價格43.84
時間2021年1月   價格45.67
時間2021年2月   價格42.86
時間2021年3月   價格51.48
時間2021年4月   價格52.94
時間2021年5月   價格46.69

你也可以用td去拆,但如果你要把表格變成自己能操作的List/tuple的話,還是學pandas來串比較快

0
uobik
iT邦新手 4 級 ‧ 2021-07-08 16:41:02

參數是可以使用變數的,要換text的值可用for遍歷

for key in XXX:
    print(key, table[0].find("td", text = key).find_next_sibling("td").text)

另外想用find_next一直print出td的話,可以這樣

td = table[0].find_next('td')

while td:
    print(td.text)
    td = td.find_next('td')

再找方法區分時間跟價格

while td:
    if '月' in td.text:
        print(td.text ,td.find_next('td').text)
    td = td.find_next('td')

https://ithelp.ithome.com.tw/upload/images/20210708/201389822sc8hFHykm.png

我要發表回答

立即登入回答