繁体   English   中英

Python Webscraping:需要帮助从 span html 标签获取数据值

[英]Python Webscraping: Need help acquiring data-value from span html tag

我目前正在尝试获取四舍五入美元的数据测试值,但我不断收到错误 NoneType,我只是想知道如何解决这个问题,谢谢 :D我想抓取的部分的网站 html 代码片段

这是我当前的代码:

import requests
from bs4 import BeautifulSoup

url = 'https://www.priceline.com/m/fly/search/YYZ-YUL-20200214/?cabin-class=ECO&no-date-search=false&search-type=11&num-adults=1&refclickid=https%3A%2F%2Fwww.google.com%2F'

page = requests.get(url)
soup = BeautifulSoup(page.content, 'html.parser')
price = soup.find('span', attrs={'data-test': 'rounded-dollars'})
print(price)

您将需要 selenium 来获取页面上显示的确切值,因为此页面的数据使用JSON Javascript动态填充。

from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument("--headless")
driver = webdriver.Chrome(executable_path=r"***YOUR_CHROME-DRIVER_PATH***", chrome_options=chrome_options)
driver.get('https://www.priceline.com/m/fly/search/YYZ-YUL-20200214/?cabin-class=ECO&no-date-search=false&search-type=11&num-adults=1&refclickid=https%3A%2F%2Fwww.google.com%2F')

soup = BeautifulSoup(driver.page_source, 'lxml')
price = soup.find_all('span', {'data-test': 'rounded-dollars'})
for price in prices: print(price.text, end=' | ')

输出:

112 | 112 | 112 | 112 | 112 | 112

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM