【问题标题】:How to web-scraping from a website where there is a loading table?如何从有加载表的网站进行网络抓取?
【发布时间】: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


【解决方案1】:

我也面临这个问题..你可以使用 python selenium 包。 我们需要等待加载您的表格,所以我使用了 time.sleep () 但这不是正确的方法。您可以使用 wait.until("element") 方法 PFB 登录示例代码

from bs4 import BeautifulSoup
from selenium import webdriver
import time
profile=webdriver.FirefoxProfile()
profile.set_preference("intl.accept_languages","en-us")
driver = webdriver.Firefox(firefox_profile=profile)
driver.get("https://www.flightradar24.com/data/airports/bud/arrivals")
time.sleep(10)
html_source=driver.page_source
soup=BeautifulSoup(html_source,"html.parser")
print soup

参考链接。

Selenium waitForElement

【讨论】:

  • 如果我使用 time.sleep 会有风险吗? time.sleep(10) 就足够了还是取决于硬件和互联网连接?
  • 是的,我知道这就是为什么我提到这个不正确的方式......所以我们可以使用 selenium api wait.untill() 这个方法等到表格内容(表格元素)填充..
  • 嗯。我试图在“page =urllib2 ....”下插入这个 time.sleep 但我收到了这个错误消息: webdriver.Firefox.implicitly_wait(30) TypeError: unbound method implicitly_wait() must be called with WebDriver instance as first argument (取而代之的是 int 实例)这是代码:webdriver.Firefox.implicitly_wait(30)
  • time.sleep 不适合 urllib。你需要使用 selenium 包
  • 呃,你可以用我的代码在你的答案中更新它以获得可读版本吗?我可以接受它
猜你喜欢
  • 2017-12-30
  • 1970-01-01
  • 2017-10-16
  • 1970-01-01
  • 1970-01-01
  • 2021-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多