我正在尝试解析篮球的杂项统计表-参考网站(https://www.basketball-reference.com/leagues/NBA_1980.html)。但是,我想解析的表在html注释中。你知道吗
使用以下代码
html = requests.get("http://www.basketball-reference.com/leagues/NBA_2016.html").content
cleaned_soup = BeautifulSoup(re.sub("<!--|-->","", html))
结果如下
TypeError Traceback (most recent call last)
<ipython-input-35-93508687bbc6> in <module>()
----> 1 cleaned_soup = BeautifulSoup(re.sub("<!--|-->","", html))
~/.pyenv/versions/3.7.0/lib/python3.7/re.py in sub(pattern, repl, string, count, flags)
190 a callable, it's passed the Match object and must return
191 a replacement string to be used."""
--> 192 return _compile(pattern, flags).sub(repl, string, count)
193
194 def subn(pattern, repl, string, count=0, flags=0):
TypeError: cannot use a string pattern on a bytes-like object
我用的是python3.7。你知道吗
与其尝试使用
re
将注释中的所有HTML放到HTML中,不如使用BeautifulSoup只返回HTML中的注释。然后还可以使用BeautifulSoup解析这些元素,以根据需要提取任何表元素,例如:这将在表中为您提供以下行:
注意:为了避免获得
cannot use a string pattern on a bytes-like object
,可以使用.text
而不是.content
将字符串传递给正则表达式。你知道吗相关问题 更多 >
编程相关推荐