【问题标题】:How to extract table value from using BeautifulSoup如何从使用 BeautifulSoup 中提取表值
【发布时间】:2021-02-08 05:31:19
【问题描述】:

我正在尝试从this table 中提取太阳经度值

我正在使用这段代码查看表的结构:

import requests
from bs4 import BeautifulSoup
URL = 'https://viewer.mars.asu.edu/viewer/themis#P=V77388006&T=2'
page = requests.get(URL)
soup = BeautifulSoup(page.content, 'html.parser')
print(soup.prettify())

但是,当我查看输出并尝试查找太阳经度时,它不存在。我什至尝试将代码的输出保存为 .txt 文件并得到相同的结果。我确实注意到我的输出比我在浏览器中看到的实际 HTML 代码要短得多。

我错过了什么吗?

【问题讨论】:

    标签: python-3.x web-scraping beautifulsoup


    【解决方案1】:

    您可能无法通过请求返回所有内容,因为它是由网站动态提供的,但您可以使用 selenium 来解决这个问题。

    示例

    from selenium import webdriver
    from bs4 import BeautifulSoup
    
    
    driver = webdriver.Chrome(executable_path=r'C:\Program Files\ChromeDriver\chromedriver.exe')
    url = 'https://viewer.mars.asu.edu/viewer/themis#P=V77388006&T=2'
    driver.get(url)
    
    soup = BeautifulSoup(driver.page_source, 'html.parser')
    driver.close()
    
    soup.select_one('[data-field="Solar Longitude"]').parent.nextSibling.get_text()
    

    输出

    30.528906°
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多