簡體   English   中英

從網站抓取表格數據

[英]Scrape Table Data from Website

我正在嘗試使用BeautifulSoup4和Python從網站上抓取表格數據,然后使用結果創建一個Excel文檔。 到目前為止,我有這個:

import urllib2
from bs4 import BeautifulSoup

soup = BeautifulSoup(urllib2.urlopen('http://opl.tmhp.com/ProviderManager/SearchResults.aspx?TPI=&OfficeHrs=4&ProgType=STAR&UCCIndicator=No+Preference&Cnty=&NPI=&Srvs=6&Age=All&Gndr=B&SortBy=Distance&ZipCd=78552&SrvsOfrd=0&SpecCd=0&Name=&CntySrvd=0&Plan=H3&WvrProg=0&SubSpecCd=0&AcptPnt=Y&Rad=200&LangCd=99').read())

for row in soup('table', {'class' : 'spad'})[0].tbody('tr'):
    tds = row('td')
    print tds[0].string, tds[1].string

但它無法顯示數據。

有任何想法嗎?

首先,該類是StandardResultsGrid ,而不是spad

其次,你不需要tbody的事情。 只需使用:

for row in soup('table', {'class' : 'StandardResultsGrid'})[0]('tr'):

還要注意,因為在原來的頁面標題行包含在tbody出於某種原因,你必須跳過第一行,所以

for row in soup('table', {'class' : 'StandardResultsGrid'})[0]('tr')[1:]

並請注意,某些單元格中包含table ,因此您必須仔細解析td的內容。

試圖從網站上抓取一張桌子<div tags< div><div id="text_translate"><p> 我正在嘗試刮這張桌子<a href="https://momentranks.com/topshot/account/mariodustice?limit=250" rel="nofollow noreferrer">https://momentranks.com/topshot/account/mariodustice?limit=250</a></p><p> 我試過這個:</p><pre> import requests from bs4 import BeautifulSoup url = 'https://momentranks.com/topshot/account/mariodustice?limit=250' page = requests.get(url) soup = BeautifulSoup(page.content, 'lxml') table = soup.find_all('table', attrs={'class':'Table_tr__1JI4P'})</pre><p> 但它返回一個空列表。 有人可以就如何解決這個問題提供建議嗎?</p></div></div>

[英]Trying to scrape a table from a website with <div tags

暫無
暫無

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

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