从网站的一个特定表中获取值

2024-09-29 17:18:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从this website得到平均价格。从“2021年古代战斗罐——MP21-EN143”中,表格位于“图片不可用”图像下方。 Orange Box outlines the able I am interested in

当我使用下面的代码时,我只是从URL顶部的第一个表,即“锦标赛资格”下面的表中获取内容

import pandas as pd
from bs4 import BeautifulSoup
import requests

url = 'https://yugiohprices.com/card_price?name=Triple+Tactics+Talent'
r = requests.get(url)
soup = BeautifulSoup(r.text, 'lxml')
table = soup.findAll('table',{'id':'item_stats'})
print(table)

是不是我的代码等待整个网站加载的时间不够长?在请求url后,我尝试等待了5秒钟,但它仍然在顶部显示第一个表。关于如何获取此值,有什么建议吗


Tags: 代码图像importurltable图片websitethis
1条回答
网友
1楼 · 发布于 2024-09-29 17:18:25

发生了什么事?

站点正在动态加载您想要获取的内容-因为请求无法模拟浏览器行为,等待不起作用

如何修复?

信息来自另一个url,您可以选择:

import pandas as pd
from bs4 import BeautifulSoup
import requests
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36'}
url = 'https://yugiohprices.com/get_card_prices/Triple%20Tactics%20Talent'
r = requests.get(url, headers= headers)
soup = BeautifulSoup(r.text, 'lxml')
soup.select('table#item_stats tr:nth-of-type(3) td')[1].text 

如果您想深入了解,另一种可能更好的解决方案是selenium,这样您就可以模拟浏览器

相关问题 更多 >

    热门问题