【问题标题】:Python BeautifulSoup - Table return none when scraping by idPython BeautifulSoup - 按id抓取时表不返回
【发布时间】:2018-12-08 05:09:39
【问题描述】:
我想从下面给定的 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)
但这是返回无。我怎样才能刮桌子?
【问题讨论】:
标签:
python-3.x
web-scraping
beautifulsoup
【解决方案1】:
它是动态页面,你可以使用来自 URL 的 json 数据,比如
https://api.weather.com/v1/geocode/12.99361134/80.17694092/observations/historical.json?apiKey=*********&startDate=20170101&endDate=20170101&units=e
您可以在浏览器控制台 -> 网络中看到真正的 API 密钥
或使用硒
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)