代码之家  ›  专栏  ›  技术社区  ›  Surabhi Choudhary

python beautifulsoup-table按id报废时不返回

  •  0
  • Surabhi Choudhary  · 技术社区  · 7 年前

    我想从下面给出的URL中抓取每日观察表 https://www.wunderground.com/history/daily/in/chennai/VOMM/date/2017-1-1

    我想用表ID来报废。我用这个密码

    from bs4 import BeautifulSoup
    import requests
    import lxml
    
    url = 'https://www.wunderground.com/history/daily/in/chennai/VOMM/date/2017-1-1';
    content = requests.get(url).content
    soup = BeautifulSoup(content, 'lxml')
    table = soup.find('table', {'id' : 'history-observation-table'})
    print(table)
    

    但这是没有回报的。我怎么刮桌子?

    1 回复  |  直到 7 年前
        1
  •  1
  •   ewwink    7 年前

    它是动态页面,您可以使用来自url的json数据,比如

    https://api.weather.com/v1/geocode/12.99361134/80.17694092/observations/historical.json?apiKey=*********&startDate=20170101&endDate=20170101&units=e
    

    您可以在浏览器控制台-网络中看到真正的API密钥IT

    或使用硒

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait 
    
    driver = webdriver.Chrome()
    driver.get("https://www.wunderground.com/history/daily/in/chennai/VOMM/date/2017-1-1")
    
    table = WebDriverWait(driver, 15).until(lambda d: d.find_element_by_id('history-observation-table'))
    print(table.text)
    
    推荐文章