【问题标题】:Scrape address using BeautifulSoup for Python使用 BeautifulSoup for Python 抓取地址
【发布时间】:2013-12-03 21:46:08
【问题描述】:

我在从以下链接中抓取地址时遇到困难,请帮我抓取地址。

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

以上网页链接的源代码如下

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

我试图在&lt;/div&gt; 之后立即刮取价值

我当前的代码没有完成,但看起来像跟随

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

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

【问题讨论】:

    标签: python beautifulsoup scrape


    【解决方案1】:

    文本是&lt;div&gt; 元素的下一个兄弟,所以使用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
    

    像这样运行它:

    python2 script.py
    

    它产生:

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

    【讨论】:

      【解决方案2】:

      这是因为 JavaScript 造成的,你应该使用 selenium webdriver 来解决这个问题:

      from selenium.webdriver import Firefox
      

      在此处了解更多信息 Link

      【讨论】:

      • 我认为你在这个上跳到硒有点快。接受的答案显示了如何在没有的情况下完成
      猜你喜欢
      • 1970-01-01
      • 2015-05-25
      • 1970-01-01
      • 1970-01-01
      • 2016-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-29
      相关资源
      最近更新 更多