簡體   English   中英

使用Python的精美湯進行網頁搜刮-JavaScript表

[英]Web Scraping with Beautiful Soup in Python - JavaScript Table

我試圖從網站上抓一張桌子,但我似乎無法用Python中的Beautifulsoup弄清楚它。 我不確定是否是因為表格格式,但是我基本上想將此表格轉換為CSV。

from bs4 import BeautifulSoup
import requests

page = requests.geenter code heret("https://spotwx.com/products/grib_index.php?model=hrrr_wrfprsf&lat=41.03399&lon=-73.76291&tz=America/New_York&display=table")
soup = BeautifulSoup(page.content, 'html.parser')
print(soup.prettify)

關於如何隔離此數據表的任何建議? 我檢查了許多Beautifulsoup教程,但是HTML看起來與大多數參考書不同。 在此先感謝您的幫助-

嘗試這個。 該站點的表是動態生成的,因此您不能僅使用requests獲得結果。

from selenium import webdriver
from bs4 import BeautifulSoup
import csv

link = "https://spotwx.com/products/grib_index.php?model=hrrr_wrfprsf&lat=41.03399&lon=-73.76291&tz=America/New_York&display=table"

with open("spotwx.csv", "w", newline='') as infile:
    writer = csv.writer(infile)
    writer.writerow(['DateTime','Tmp','Dpt','Rh','Wh','Wd','Wg','Apcp','Slp'])
    with webdriver.Chrome() as driver:
        driver.get(link)
        soup = BeautifulSoup(driver.page_source, 'lxml')
        for item in soup.select("table#example tbody tr"):
            data = [elem.text for elem in item.select('td')]
            print(data)
            writer.writerow(data)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM