2017-12-27 18 views
0

件名として、私はbeautifulsoupを使用してテーブルをフェッチしようとしています。印刷(細胞)は、テーブル内のすべてのTDを返すことができないが Python beautifulsoup paring html table-td data missing

http://www.hkjc.com/english/racing/Horse.asp?HorseNo=T421

from selenium import webdriver 
    from bs4 import BeautifulSoup 
    import pandas as pd 
    import numpy as np 
    import lxml 
    import xlrd 

    HorseNo = ["T421"] 
    driver = webdriver.PhantomJS(r'D:\Program Files\Python\Path\PhantomJS\bin\phantomjs.exe') 
    #driver = webdriver.Chrome(r'D:\Program Files\Python\Path\chromedriver.exe') 
    url = "http://www.hkjc.com/english/racing/horse.asp?HorseNo=" + str(HorseNo) 
    driver.get(url) 
    html = driver.page_source 
    soup = BeautifulSoup(html, "lxml") 
    table = soup.find("table", {"class" :"bigborder", "width":"970"}).findAll("tr") 
    print(table) 
    for row in table: 
     cells = row.findAll("td") 

    print(cells) 

印刷(表)、結果は大丈夫です。誰かが私にさらに助言しますか?ありがとう。

答えて

0

requests

from bs4 import BeautifulSoup 
import requests 

HorseNo = ["T421"] 
url = "http://www.hkjc.com/english/racing/horse.asp?HorseNo=" + str(HorseNo) 
html = requests.get(url).text 
soup = BeautifulSoup(html, "lxml") 
table = soup.find("table", {"class" :"bigborder", "width":"970"}).findAll("tr") 
cells = [] 
for row in table: 
    cell = row.findAll("td") 
    cells.append(cell) 

print(cells) 
+0

おかげで、ジョンを用いて以下にこれを試してみてください。同じ結果を試しました。ところで、リクエストとセレンのドライバを使用する違いは何ですか? – Wyne

+0

はいごめんなさい、アップデートを追加するのを忘れてしまいました。もう一度お試しいただけますか?私はより軽量であると思う要求は – johnII

+0

ありがとう、それは働いている。しかし、なぜ私の学問的興味のために細胞= []が必要ですか? – Wyne