【发布时间】:2017-06-11 12:16:23
【问题描述】:
我已经从 python 的页面中删除了数据。我能够打印表格标签的内容。但在那之后,我无法将其作为列表获取并将其转换为数据框。这是我的代码-
import urllib2
import pandas as pd
wiki = "https://www.marinetraffic.com/en/ais/index/ships/all/per_page:50/page:1"
hdr = {'User-Agent': 'Mozilla/5.0'}
req = urllib2.Request(wiki,headers=hdr)
page = urllib2.urlopen(req)
from bs4 import BeautifulSoup
soup = BeautifulSoup(page, "html.parser")
#print soup.prettify()
print soup.title.string
#print soup.a
all_links = soup.find_all("a")
for link in all_links:
link.get("href")
all_tables=soup.find_all('table')
right_table=soup.find('table', class_='table table-hover text-left')
print right_table
#Generate lists
A=[]
B=[]
C=[]
D=[]
E=[]
F=[]
G=[]
for row in right_table.findAll("tr"):
cells = row.findAll('td')
states=row.findAll('img') #To store second column data
if len(cells)==12: #Only extract table body not heading
A.append(states[0].find(text=True))
B.append(cells[0].find(text=True))
C.append(cells[2].find(text=True))
D.append(cells[3].find(text=True))
E.append(cells[4].find(text=True))
F.append(states[1].find(text=True))
G.append(cells[6].find(text=True))
df=pd.DataFrame(A,columns=['Flag'])
df['Vessel ID']=B
df['MMSI']=C
df['Vessel Name']=D
df['Photo']=E
df['Type']=F
df['Latest Position']=G
print df
它只给出一个字段的数据。其余字段为空或不提供。 请帮忙。提前致谢。
编辑:我想要像this image 这样的数据 但我得到了输出like this.
【问题讨论】:
标签: python python-2.7 web-scraping beautifulsoup