使用BeautifulSoup for Python获取地址

2024-09-30 18:18:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我有困难从下面的网站链接刮地址,请帮我刮地址。在

http://www.salatomatic.com/d/Revesby+17154+Ahlus-Sunnah-Wal-Jamaah-Revesby

上面的weblink的源代码如下

<td width="100%"><div class="titleBM">Bankstown Masjid </div>Meredith Street, Bankstown, New South Wales 2200</td>

我试图在</div>之后立即获取该值

我当前的代码还没有完成,但看起来像下面这样

^{pr2}$

Tags: divcomhttp网站链接地址wwwtd
2条回答

这是因为JavaScript,您应该使用selenium webdriver来解决这个问题:

from selenium.webdriver import Firefox

在此处查找更多信息Link

文本是<div>元素的下一个同级,因此使用next_sibling

from bs4 import BeautifulSoup
import urllib2
import datetime

url1 = 'http://www.salatomatic.com/d/Revesby+17154+Ahlus-Sunnah-Wal-Jamaah-Revesby'

content1 = urllib2.urlopen(url1).read()
soup1 = BeautifulSoup(content1)
div1 = soup1.find('div', {'class':'titleBM'}) #get the div where it's located
pos1 = div1.next_sibling

print datetime.datetime.now(), 'street address:  ' , pos1

运行方式如下:

^{pr2}$

它产生:

2013-12-03 12:55:41.306271 street address:   9-11 Mavis Street, Revesby, New South Wales 2212

相关问题 更多 >