如何使用 Python 和 Beautiful Soup 从框架中抓取信息

Rob*_*ank 4 python screen-scraping beautifulsoup

这是我第一次尝试网络抓取。我正在尝试使用 Beautiful Soup 从 Raymond James 的网站上抓取电话号码。一个例子是http://www.raymondjames.com/office_locator_display.asp?addressline=90210

每当我使用 BeautifulSoup 时,我都无法在 HTML 中找到适当的信息。

import urllib2
from bs4 import BeautifulSoup

url='http://www.raymondjames.com/office_locator_display.asp?addressline=90210'

opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3)        AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36')]
page_to_scrape=opener.open(url).read()
soup=BeautifulSoup(page_to_scrape.decode('utf-8','ignore'))
Run Code Online (Sandbox Code Playgroud)

生成的输出不包含我需要的信息。我提供的 URL 似乎没有指向位置框架。

我不使用 Python 来处理 Web 数据的大量工作,因此我不知道如何将 Beautiful Soup 引导到“框架”中以获得联系信息。

mat*_*exx 5

正如 Martijn 所说,挖掘网络请求,源数据就在那里。在本例中,它是对 iframe 中发出的 GET 请求的 xml 响应。有了这个 url,解决方案就非常简单:

import urllib2
from bs4 import BeautifulSoup
soup = BeautifulSoup(urllib2.urlopen('http://hosted.where2getit.com/raymondjames/ajax?&xml_request=%3Crequest%3E%3Cappkey%3E7BD67064-FC36-11E0-B80D-3AEEDDB2B31E%3C%2Fappkey%3E%3Cformdata+id%3D%22locatorsearch%22%3E%3Cdataview%3Estore_default%3C%2Fdataview%3E%3Climit%3E30%3C%2Flimit%3E%3Cgeolocs%3E%3Cgeoloc%3E%3Caddressline%3E90210%3C%2Faddressline%3E%3Clongitude%3E%3C%2Flongitude%3E%3Clatitude%3E%3C%2Flatitude%3E%3Ccountry%3E%3C%2Fcountry%3E%3C%2Fgeoloc%3E%3C%2Fgeolocs%3E%3Csearchradius%3E25%7C50%7C100%3C%2Fsearchradius%3E%3C%2Fformdata%3E%3C%2Frequest%3E'), 'lxml')
# parse the points of interest into a list
pois = soup.find_all('poi')
# now have your way with them!
Run Code Online (Sandbox Code Playgroud)