[英]Beautiful Soup not finding CSS selector
我正在使用Beautiful Soup and Requests嘗試從網站上抓取數據,並且在使用特定CSS選擇器提取數據時遇到困難。 我正在使用SelectorGadget( https://selectorgadget.com/ )來標識我在此站點上正在尋找的選擇器: https: .op-bovada.\\lv
它。 但是,這不起作用,也不能轉義反斜杠。 我已經嘗試了多種變體,並在網上搜尋,但是沒有任何運氣。 我是python的初學者,所以我有一種直覺,我忽略了一些明顯的東西。
這段代碼重現了我遇到的問題。
import requests, bs4
res = requests.get('https://www.oddsshark.com/ncaab/odds')
odds = bs4.BeautifulSoup(res.text, 'html.parser')
# This, another identifier from the same site, works fine.
print(str(len(odds.select('.op-opening'))))
# However, this does not.
print(str(len(odds.select('.op-bovada.\lv'))))
print(str(len(odds.select('.op-bovada.\\lv'))))
我在R中執行此操作沒有問題-它只需要雙反斜杠,所以我知道那里有數據,但此刻我在腦海中猶豫不決。
為什么不使用漂亮的湯尋找方法?
print(len(odds.find_all(class_='op-bovada.lv')))
問題是由select解析傳入的字符串的方式引起的。 標識一個類,但是在這種情況下,。 是類的一部分,因此解析器無法正確解釋它。 通過。 放入find_all的類參數中,您將獲得所需的效果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.