【发布时间】:2017-12-30 23:03:38
【问题描述】:
我尝试从一个使用 Python 2.7 的网站进行网络抓取,其中有一个必须加载的表。如果我试图对它进行网络抓取,我只会得到:“正在加载”或“对不起,我们没有任何关于它的信息”,因为它必须先加载..
我阅读了一些文章和代码,但没有任何效果。
我的代码:
import urllib2, sys
from BeautifulSoup import BeautifulSoup
import json
site= "https://www.flightradar24.com/data/airports/bud/arrivals"
hdr = {'User-Agent': 'Mozilla/5.0'}
req = urllib2.Request(site,headers=hdr)
page = urllib2.urlopen(req)
soup = BeautifulSoup(page)
nev = soup.find('h1' , attrs={'class' : 'airport-name'})
print nev
table = soup.find('div', { "class" : "row cnt-schedule-table" })
print table
import urllib2
from bs4 import BeautifulSoup
import json
# new url
url = 'https://www.flightradar24.com/data/airports/bud/arrivals'
# read all data
page = urllib2.urlopen(url).read()
# convert json text to python dictionary
data = json.loads(page)
print(data['row cnt-schedule-table'])
【问题讨论】:
-
该数据通常由 ajax 加载,有时来自 javascript 的 vars。您需要找到来源并从中获取信息。
-
使用 fiddler、charles proxy 等工具。对于这种情况,这是您的 ajax api 调用 api.flightradar24.com/common/v1/…
-
这个链接对我来说不是个好主意,因为这样会丢失一些信息。
标签: python json web-scraping beautifulsoup