看板 Python 關於我們 聯絡資訊
※ 引述《yshihyu (yshihyu)》之銘言: : https://gist.github.com/anonymous/01d0536745d9814815081aa03d547bc7 : 上面程式碼我想針對標題是 "獲 利 狀 況" 抓取表格 : 那要如何字串比對 "獲 利 狀 況" 找道是在list裡面的哪個index? : 下面是我測試 2103 股票在第41個index, 但其他檔股票 "獲 利 狀 況" 可能不是在41 index : df2=pd.DataFrame(df[41]) : http://goodinfo.tw/StockInfo/StockDetail.asp?STOCK_ID=2103 : 謝謝 import requests from bs4 import BeautifulSoup import pandas as pd headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'} res = requests.get('http://goodinfo.tw/StockInfo/StockDetail.asp?STOCK_ID=2103', headers=headers) res.encoding = 'utf-8' soup = BeautifulSoup(res.text.replace(' ', '').replace(' ',''), 'lxml') for head in soup.select('.head_td b'): if head.text == '合併獲利狀況': df = pd.read_html(str(head.parent.parent.parent.parent)) -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 118.160.52.219 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1487499902.A.C6C.html