2017-06-18 35 views
1

我目前正在学习使用Python和Beautiful Soup进行网页报废。我给出的任务,其网页被有星级CSS伪元素使用BeautifulSoup4从CSS3伪元素获取内容

<span class="bb_rating bble_50"> 
    ::before 
    ::after 
</span> 

bble_50::after { 
    content: "\e00b\e00b\e00b\e00b\e00b"; 
} 

enter image description here

我想知道我怎样才能从CSS伪元素里面的内容? 需要帮助。谢谢

+0

看起来像一个旅行顾问。是不是?你能展示一下你如何获得页面源代码?谢谢。 – alecxe

+0

是的,它来自旅行顾问,但我的导师只是改变了一些CSS,所以我不能从任何地方复制粘贴。 :) – raju

回答

1

我不认为你应该实际去解析CSS在这里。只需按类别划分出的类名

class_to_rating = { 
    "bble_45": 4.5, 
    "bble_50": 5 
} 
elm = soup.select_one(".bb_rating") 
rating_class = next(value for value in elm["class"] if value.startswith("bble_")) 

print(class_to_rating.get(rating_class, "Unknown rating")) 
+0

听起来不错。谢谢你。但是有没有什么办法可以实际获得伪选择器。我可以使用javascript,如果我使用Node.js – raju

+0

@raju,那么CSS解析器就像['tinycss'](https://tinycss.readthedocs.io/en/latest/)。总是有正则表达式。而且,如果'selenium'是一个选择,它可能有助于通过['value_of_css_property'](http://selenium-python.readthedocs.io/api.html#selenium.webdriver.remote)到达“content”css属性.webelement.WebElement.value_of_css_property).. – alecxe

+0

非常感谢。这有帮助。在“partial_entry”类中点击“更多”后,如何才能获得内容?我应该为此打开另一个问题。 – raju