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

时间:2013-12-22 02:37:11

标签: python beautifulsoup screen-scraping

这是我第一次尝试网络抓取。我正在尝试使用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'))

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

我不会将Python用于网络数据的大量工作,所以我不知道如何将美丽的汤指向“框架”以获取联系信息。

1 个答案:

答案 0 :(得分:2)

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

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!