【问题标题】:Web-scraping - CSS Selector returning 0网页抓取 - CSS 选择器返回 0
【发布时间】:2021-10-26 00:56:40
【问题描述】:

我正在关注 Udemy 上的自动化无聊东西课程,并试图从币安获取比特币的价格。但是,当使用 CSS 选择器时,我得到的值始终为 0。

 import requests, bs4
 res = requests.get('https://www.binance.com/en')
 res.raise_for_status()
 
 soup = bs4.BeautifulSoup(res.text)
 elems = soup.select('#top_crypto_table-2-BTC_BUSD > div.css-11d5f40')
 
 elems[0].text.strip()
  
 '0'

【问题讨论】:

  • 你好,布莱克,你能澄清一下期望值是多少吗?
  • 我认为您无法获得这些值,它们在不断变化。尝试硒

标签: python css web-scraping


【解决方案1】:

BeautifulSoup 在这种情况下不起作用,因为 JavaScript 会不断更新这些值。

您需要尝试 Selenium 并获取数据。

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import requests, bs4

options = Options()
options.add_argument('--headless')

driver = webdriver.Chrome(executable_path=r"C:\Users\User\Downloads\chromedriver.exe", options=options)


driver.get('https://www.binance.com/en')

 
soup = bs4.BeautifulSoup(driver.page_source, 'lxml')
elems = soup.select_one('#top_crypto_table-2-BTC_BUSD > div.css-11d5f40')
print(elems.text.strip())

目前的价值。

$46,877.79

【讨论】:

    猜你喜欢
    • 2021-10-25
    • 2021-05-29
    • 1970-01-01
    • 2019-02-22
    • 1970-01-01
    • 1970-01-01
    • 2018-12-18
    • 2021-09-25
    • 2022-01-19
    相关资源
    最近更新 更多