Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法识别BeautifulSoup注释表中的差异_Python_Html_Python 3.x_Web Scraping_Beautifulsoup - Fatal编程技术网

Python 无法识别BeautifulSoup注释表中的差异

Python 无法识别BeautifulSoup注释表中的差异,python,html,python-3.x,web-scraping,beautifulsoup,Python,Html,Python 3.x,Web Scraping,Beautifulsoup,我知道这个网站上还有一些关于已注释HTML表的问题,但我不相信它们能回答这个特定的问题 我正设法把所有的桌子都弄到手。除了其中一个,我都能拍到“射击” 我知道,除了“每场比赛”之外,其他所有内容都在页面源代码中被注释掉了。为了处理注释掉的表,我使用如下代码: soup = BeautifulSoup(res.text, "lxml") comment_table = soup.find(text=lambda x: isinstance(x, NavigableString)

我知道这个网站上还有一些关于已注释HTML表的问题,但我不相信它们能回答这个特定的问题

我正设法把所有的桌子都弄到手。除了其中一个,我都能拍到“射击”

我知道,除了“每场比赛”之外,其他所有内容都在页面源代码中被注释掉了。为了处理注释掉的表,我使用如下代码:

soup = BeautifulSoup(res.text, "lxml")
comment_table = soup.find(text=lambda x: isinstance(x, NavigableString) and stat in x)
soup = BeautifulSoup(comment_table, "lxml")
table = soup.find("table", id=stat)
stat
可以是任意数量的“stats”。有效的是“每分钟”、“pbp”、“调整拍摄”等

它首先找到已注释的表,然后继续按照正常方式解析该表。对于所有已注释的表,
table
是一个表标记。然而,当
stat
为“shooting”时,
为空

当查找包含“shooting”的实例时,
comment_表
行将返回文本
'Player shooting history'
,而不是预期的表。我已经验证了在注释掉的文本中(在页面源代码中)有“射击”


有人能帮我找出这个表有什么不同吗?

它与前面的一个实例相匹配,该实例中存在该字符串。当您在id中查找它时,我认为您可以更改以下行:

comment_table = soup.find(text=lambda x: isinstance(x, NavigableString) and stat in x)



例如,您可以使用正则表达式对主题进行变体。这样做是为了确保您在id属性中查找该值。

您是否检查过它不是js创建的内容?几个月前我浏览了这个页面,遇到了一些类似的问题。我无法想象这一张表会是什么样子,但我该如何检查呢?它在页面源代码中。谢谢,我确实尝试过这个和
…以及“id=shooting”…
但是没有找到任何东西。在我的示例中,它似乎对我有用。没有为你的其他人测试。哦,我让你的代码工作。f字符串中的单双引号是什么意思?我对此不熟悉。这是因为属性应该是id=”“,我只是在第二个“无忧”之前提前关闭了。希望这对你的其他人继续有效。
comment_table = soup.find(text=lambda x: isinstance(x, NavigableString) and f'id="{stat}' in x)
from bs4 import BeautifulSoup as bs
from bs4 import NavigableString
import requests

res = requests.get('https://www.basketball-reference.com/players/j/jamesle01.html')
soup = bs(res.text, "lxml")
stat = 'shooting'
comment_table = soup.find(text=lambda x: isinstance(x, NavigableString) and f'id="{stat}' in x)
soup = bs(comment_table, "lxml")
table = soup.find("table", id=stat)
print(table)