Python BeautifulSoup提取Titile Web爬虫

时间:2017-01-24 09:28:41

标签: python html beautifulsoup web-crawler

我正在尝试从图像中提取标题。我设法提取url,但不知道如何编码提取图像标题。

Code:

import requests 
from bs4 import BeautifulSoup 

def trade_spider(max_pages): 
    page = 1
    while page <= max_pages:
        url = 'http://www.gurstree.com.au/s—cars—vans—utes/melbourne/page—' + str(page) + '/c1832013001317'
        source_code = requests.get(url)
        plain_text = source_code.text
        soup = BeautifulSoup(plain_text)
        for link in soup.findAll('a', {'class': 'ad—listing_title—link'}):
            href = 'http://www.gumtree.com.au/' + link.get('href')
            print(href)
        page += 1 

trade_spider(1)

The HTML is:

<a itemprop="url" class="ad-listing__thumb-link" name="1124692138" href="/s-ad/derrimut/cars-vans-utes/2015-toyota-86-coupe-12-month-warranty-/1124692138" data-ref="searchTopAd">
  <span id="r-image-TOP_AD-1124692138" title="2015 Toyota 86 Coupe **12 MONTH WARRANTY** Derrimut Brimbank Area Preview" class="j-responsive-image ad-listing__thumb" data-index="1">...</span>
</a>

第一行是href,但我希望根据突出显示的title HTML块获取span

谢谢!

1 个答案:

答案 0 :(得分:0)

link.span.get('title')

使用.查找下一个span并获取title

使用regex匹配addribute中的字符串:

import re    
soup.find('span', id=re.compile(r'r-image'))