【问题标题】:Python BeautifulSoup Extracting Titile Web Crawler [duplicate]Python BeautifulSoup 提取标题网络爬虫 [重复]
【发布时间】:2017-06-09 00:50:29
【问题描述】:

我正在尝试从图像中提取标题。我设法提取了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,但我想根据span HTML 块突出显示title

谢谢!

【问题讨论】:

  • 发布你的代码而不是图片
  • 你可以在这里添加网址吗?很难从代码图像中得到

标签: python html beautifulsoup web-crawler


【解决方案1】:
link.span.get('title')

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

使用regex匹配地址中的字符串:

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

【讨论】:

  • 好的,我设法让它与 link.get('title') 一起工作。如果我想使用 'id' 参考和 'r-image-TOP_AD-1124692138',如果每个帖子的 -Top_AD- 末尾的数字都更改了,我该如何使用它?
  • 太棒了,谢谢!
猜你喜欢
  • 2015-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多