gpt4 book ai didi

python - 解析::之前使用 BS4

转载 作者:行者123 更新时间:2023-12-05 06:57:43 24 4
gpt4 key购买 nike

尝试解析 a web page .在 Page html 中面对 ::before

url = 'https://kant-sport.ru/sports/skiing/svobodnoe-katanie/'

# Getting whole page
page = get(url)

# Making soup
soup = BS(page.content, 'html.parser')

# Getting table
table = soup.select('.new-tables-content')[0]

# Getting table's rows and getting rid of first unneeded row
rows = table.select('.new-tables-content-row')[1:]

然后我需要得到'x'符号

# Getting 'x' symbol by class
print(rows[0].find(class_="new-tables-content-col::before"))

输出

None

并使用选择方法(css selector )

# Getting 'x' symbol by css
print(rows[0].select('.new-tables-content-row:not(.new-tables-content-header) .new-tables-content-col:last-child:before'))

输出

Traceback (most recent call last):
File "E:/Coding/PycharmProjects/kant-monitoring-bot/parser.py", line 36, in <module>
print(rows[0].select('.new-tables-content-row:not(.new-tables-content-header) .new-tables-content-col:last-child:before'))
File "E:\Coding\PycharmProjects\kant-monitoring-bot\venv\lib\site-packages\bs4\element.py", line 1869, in select
results = soupsieve.select(selector, self, namespaces, limit, **kwargs)
File "E:\Coding\PycharmProjects\kant-monitoring-bot\venv\lib\site-packages\soupsieve\__init__.py", line 98, in select
return compile(select, namespaces, flags, **kwargs).select(tag, limit)
File "E:\Coding\PycharmProjects\kant-monitoring-bot\venv\lib\site-packages\soupsieve\__init__.py", line 62, in compile
return cp._cached_css_compile(pattern, namespaces, custom, flags)
File "E:\Coding\PycharmProjects\kant-monitoring-bot\venv\lib\site-packages\soupsieve\css_parser.py", line 208, in _cached_css_compile
CSSParser(pattern, custom=custom_selectors, flags=flags).process_selectors(),
File "E:\Coding\PycharmProjects\kant-monitoring-bot\venv\lib\site-packages\soupsieve\css_parser.py", line 1043, in process_selectors
return self.parse_selectors(self.selector_iter(self.pattern), index, flags)
File "E:\Coding\PycharmProjects\kant-monitoring-bot\venv\lib\site-packages\soupsieve\css_parser.py", line 902, in parse_selectors
has_selector, is_html = self.parse_pseudo_class(sel, m, has_selector, iselector, is_html)
File "E:\Coding\PycharmProjects\kant-monitoring-bot\venv\lib\site-packages\soupsieve\css_parser.py", line 640, in parse_pseudo_class
"'{}' pseudo-class is not implemented at this time".format(pseudo)
NotImplementedError: ':before' pseudo-class is not implemented at this time

Process finished with exit code 1

如何使用 ::before::after 正确解析元素

最佳答案

如错误所示,这尚未在您使用的解析器中实现。

要处理这个问题,您可以使用支持它的解析器,例如 lxml 解析器。

pip3 install --upgrade lxml bs4
soup = BeautufulSoup(page.content, 'lxml')

还要确保您使用的是 Python 3 和最新版本的 bs4

关于python - 解析::之前使用 BS4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64843419/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com