Python Pandas和bs4跳过已刮表中的超链接
我试图用pandas和bs4从一条MTG金鱼身上刮下一张桌子。我的长期目标是给我自己发移动者和震动者列表,但我得到了5列中的4列,但它跳过了,并且给出了一个奇怪的结果,对于一个有超链接的列。我想要的只是超链接的显示名称,这样我就可以将其作为表来读取Python Pandas和bs4跳过已刮表中的超链接,python,pandas,web-scraping,beautifulsoup,Python,Pandas,Web Scraping,Beautifulsoup,我试图用pandas和bs4从一条MTG金鱼身上刮下一张桌子。我的长期目标是给我自己发移动者和震动者列表,但我得到了5列中的4列,但它跳过了,并且给出了一个奇怪的结果,对于一个有超链接的列。我想要的只是超链接的显示名称,这样我就可以将其作为表来读取 import requests from bs4 import BeautifulSoup as bs import pandas as pd response = requests.get("https://www.mtggoldfi
import requests
from bs4 import BeautifulSoup as bs
import pandas as pd
response = requests.get("https://www.mtggoldfish.com/movers/paper/standard")
soup = bs(response.text, "html.parser")
table = soup.find_all('table')
df = pd.read_html(str(table))[0]
print(df)
问题是这个
Top Winners Top Winners.1 ... Top Winners.3 Top Winners.4
0 5.49 xznr ... $ 16.00 +52%
1 0.96 thb ... $ 18.99 +5%
2 0.63 xznr ... $ 5.46 +13%
3 0.49 m21 ... $ 4.99 +11%
4 0.41 xznr ... $ 4.45 +10%
5 0.32 xznr ... $ 17.10 +2%
6 0.25 xznr ... $ 0.71 +54%
7 0.25 xznr ... $ 0.67 +60%
8 0.15 eld ... $ 18.70 +1%
9 0.12 thb ... $ 11.87 +1%
第三列是附加到网站上卡片页面超链接的卡片名称。我想不出如何将所有内容一起提取。只要调用.to_string()
:
输出:
Top Winners Top Winners.1 Top Winners.2 Top Winners.3 Top Winners.4
0 0.96 thb Kroxa, Titan of Death's Hunger $ 18.99 +5%
1 0.63 xznr Clearwater Pathway $ 5.46 +13%
2 0.49 m21 Thieves' Guild Enforcer $ 4.99 +11%
3 0.41 xznr Skyclave Apparition $ 4.45 +10%
4 0.32 xznr Scourge of the Skyclaves $ 17.10 +2%
5 0.25 xznr Malakir Rebirth $ 0.71 +54%
6 0.25 xznr Blackbloom Rogue $ 0.67 +60%
7 0.16 xznr Zof Consumption $ 0.63 +34%
8 0.15 eld Oko, Thief of Crowns $ 18.70 +1%
9 0.12 thb Heliod, Sun-Crowned $ 11.87 +1%
您可以将html表格直接读给熊猫。flavor可以设置为“html.parser”,但“lxml”更快
将熊猫作为pd导入
tables=pd.read\u html(“https://www.mtggoldfish.com/movers/paper/standard“,flavor='lxml')
#每日变化
每日获奖者=表格[0]
每日损失=表[1]
#每周变动
每周获奖者=表[2]
每周损失人数=tablke[3]
您可以将数据直接读取到熊猫。熊猫会在后台为你做请求和美化。见下面我的答案。
Top Winners Top Winners.1 Top Winners.2 Top Winners.3 Top Winners.4
0 0.96 thb Kroxa, Titan of Death's Hunger $ 18.99 +5%
1 0.63 xznr Clearwater Pathway $ 5.46 +13%
2 0.49 m21 Thieves' Guild Enforcer $ 4.99 +11%
3 0.41 xznr Skyclave Apparition $ 4.45 +10%
4 0.32 xznr Scourge of the Skyclaves $ 17.10 +2%
5 0.25 xznr Malakir Rebirth $ 0.71 +54%
6 0.25 xznr Blackbloom Rogue $ 0.67 +60%
7 0.16 xznr Zof Consumption $ 0.63 +34%
8 0.15 eld Oko, Thief of Crowns $ 18.70 +1%
9 0.12 thb Heliod, Sun-Crowned $ 11.87 +1%