2017-12-19 8 views
-1

私は空港名のデータを削っています。 は、私は次のコードを使用しますが、私は彼らには2行いないデータを取得するPythonでテーブルを掻く/ beautifulsoup

import requests 
from bs4 import BeautifulSoup 

url = 'http://www.airlineupdate.com/content_public/codes/airportcodes/airports-by-iata/iata-a.htm' 
page_html = requests.get(url) 
page_text = page_html.text 
soup = BeautifulSoup(page_text, "html.parser") 
table = soup.find('table', {'class': 'sortable'}) 
for tr in table.findAll('tr'): 
    for tb in tr.findAll('tb'): 
     print(tb.text) 
+0

これは実際の質問ではありません。あなたはここでもう少し作業をし、答えられるようにフォーマットする必要があります。 – jugg1es

+0

あなたは何を得たいと思っていますか?何を得ていますか? – roelofs

+0

'page_html.text'を印刷して、テーブルに期待されるデータがあるページがあるかどうかを確認してください。または2つのテーブルがあるかもしれませんが、最初に取得するだけです。 – furas

答えて

0

あなたはこのコードからヒントをこのような何かをしようとして取ることができ

import requests 
from bs4 import BeautifulSoup 
import time 

url = 'http://www.airlineupdate.com/content_public/codes/airportcodes/airports-by-iata/iata-a.htm' 
page_html = requests.get(url) 
page_text = page_html.text 
soup = BeautifulSoup(page_text, "html.parser") 
table = soup.find_all('td') 
list_1=[] 
for i in table: 
    list_1.append(i.text) 

for chunk in range(0,len(list_1[10:]),5): 
    chunks=list_1[10:][chunk:chunk+5] 
    print("IATA code : {}".format(chunks[0])) 
    print("ICAO code : {}".format(chunks[1])) 
    print("Airport : {}".format(chunks[2])) 
    print("City : {}".format(chunks[3])) 
    print("Country : {}".format(chunks[4])) 

    print("---------------------------") 

出力:

IATA code : AAA 
ICAO code : NTGA 
Airport : Anaa Airport 
City : Anaa 
Country : French Polynesia 
--------------------------- 
IATA code : AAB 
ICAO code : YARY 
Airport : Arrabury Airport 
City : Arrabury 
Country : Australia 
--------------------------- 
IATA code : AAC 
ICAO code : HEAR 
Airport : Al Arish Airport 
City : Al Arish 
Country : Egypt 
--------------------------- 
IATA code : AAD 
ICAO code : - 
Airport : Ad-Dabbah Airport 
City : Ad-Dabbah 
Country : Sudan 
--------------------------- 
    ..... 
+0

ありがとうございました:D –

+0

@HossamAshraf私の答えがあなたを助けた場合、あなたは[回答を受け入れる]ことができます(https://meta.stackexchange.com/questions/86978/how-do-i-accept-an- answer-on-stackoverflow):) –

0

これがあなたの望むものかどうかはわかりません。以下のスクリプトを実行すると、そのテーブルからデータが取得されます。

import requests 
from bs4 import BeautifulSoup 

url = 'http://www.airlineupdate.com/content_public/codes/airportcodes/airports-by-iata/iata-a.htm' 
page_html = requests.get(url) 
soup = BeautifulSoup(page_html.text,"lxml") 
table = soup.find('table',class_='sortable') 
for tr in table.find_all('tr'): 
    data = ' '.join([item.text.strip() for item in tr.find_all('td')]) 
    print(data) 
0

このコードでは、都市のみが表示されます。 他のパラメータが必要な場合は、コメントを削除してください

import pandas as pd 
import requests 
from bs4 import BeautifulSoup 
from tabulate import tabulate 

"""res=requests.get("http://www.airlineupdate.com/ 
content_public/codes/airportcodes/airports-by-iata/iata-a.htm")""" 

soup = BeautifulSoup(res.content,'lxml') 

df = pd.read_html(str(table))[0] 
# IATA_code=df[0] 
# ICAO_code=df[1] 
# airport=df[2] 
city=df[3] 
# country=df[4] 

print(list(city[1:])) 
# print(list(country[1:])) 
# print(list(airport[1:])) 
# print(list(ICAO_code[1:])) 
# print(list(IATA_code[1:])) 
関連する問題