美化集团刮街道地址

2024-10-04 01:35:27 发布

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

我使用最底层的代码来获取网络链接,以及Masjid名称。不过,我也希望得到面额街道地址。请帮帮我。在

目前我得到以下

网络链接:

<div class="subtitleLink"><a href="http://www.salatomatic.com/d/Tempe+5313+Masjid-Al-Hijrah">

Masjid name

^{pr2}$

但是我想得到下面的东西

面额

<b>Denomination:</b> Sunni (Traditional)

以及街道地址

<br>45 Station Street (Sydney)&nbsp;&nbsp;

下面的代码将删除以下代码

<td width=25><a href="http://www.salatomatic.com/d/Tempe+5313+Masjid-Al-Hijrah"><img src='http://www.halalfire.com/images/en/photo_small.jpg' alt='Masjid Al-Hijrah' title='Masjid Al-Hijrah' border=0 width=48 height=36></a></a></td><td width=10><img src="http://www.salatomatic.com/images/spacer.gif" width=10 border=0></td><td nowrap><div class="subtitleLink"><a href="http://www.salatomatic.com/d/Tempe+5313+Masjid-Al-Hijrah"><b>Masjid Al-Hijrah</b></a>&nbsp;&nbsp; </div><div class="tinyLink"><b>Denomination:</b> Sunni (Traditional)<br>45 Station Street (Sydney)&nbsp;&nbsp;</div></td><td align=right valign=center><div class="tinyLink"></div></td>

代码:

from bs4 import BeautifulSoup
import urllib2

url1 = "http://www.salatomatic.com/c/Sydney+168"
content1 = urllib2.urlopen(url1).read()
soup = BeautifulSoup(content1) 

results = soup.findAll("div", {"class" : "subtitleLink"})
for result in results :
    br = result.find('b')
    a = result.find('a')
    currenturl =  a.get('href')
    if not currenturl.startswith("http"):
        currenturl = "http://www.salatomatic.com" + currenturl
        print currenturl
    elif currenturl.startswith("http"):
        print a.get('href')
    pos = br.get_text()
    print pos

Tags: 代码brdivcomhttpwwwclasstd
1条回答
网友
1楼 · 发布于 2024-10-04 01:35:27

您可以检查下一个<div>元素,该元素具有值为tinyLinkclass属性,该属性包含<b><br>标记,并提取它们的字符串:

...
print pos 
div = result.find_next_sibling('div', attrs={"class": "tinyLink"})
if div and div.b and div.br:
    print(div.b.next_sibling.string)
    print(div.br.next_sibling.string)

相关问题 更多 >