【发布时间】:2018-06-06 16:15:59
【问题描述】:
我想从下面的 HTML 中获取“8.0”:
<div class="js-otelpuani" style="float: left;"> ==$0
"8.0"
<span class="greyish" style="font-size:13px; font-
family:arial;"> /10</span>
::after
</div>
我已经尝试使用下面的代码在 div class= 'js-otelpuani' 中提取 '8.0',但它似乎不起作用;
import urllib
import requests
from bs4 import BeautifulSoup
import pyodbc
headers = {
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5)",
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"accept-charset": "cp1254,ISO-8859-9,utf-8;q=0.7,*;q=0.3",
"accept-encoding": "gzip,deflate,sdch",
"accept-language": "tr,tr-TR,en-US,en;q=0.8",
}
r = requests.get('https://www.otelz.com/otel/elvin-deluxehotel#.WkDIBd9l_IU', headers=headers)
if r.status_code != 200:
print("request denied")
else:
print("ok")
soup = BeautifulSoup(r.text)
score = soup.find('div',attrs={'class': 'js-otelpuani'})
print(score)
我将这些作为输出,但不幸的是我无法获得想要提取的“8.0”值;
ok
<div class="js-otelpuani" style="float: left;">
<span id="comRatingValue">.0</span>
<span class="greyish" style="font-size: 13px; font-family: arial;">
/
<span itemprop="bestRating">10</span></span>
<span id="comRatingCount" itemprop="ratingCount" style="display:
none;">0</span>
<span id="comReviewCount" itemprop="reviewCount" style="display:
none;">0</span>
</div>
如果有任何帮助,我将不胜感激!
【问题讨论】:
标签: python web-scraping beautifulsoup python-requests python-3.6