图像数,使用“len()”

2024-04-18 01:30:19 发布

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

我需要计算图像的数量(在本例中是1个图像)。显然是在用“len()”?在

以下是HTML:

<div class="detail-headline">
    Fotogal&#233;ria
        </div>
<div class="detail-indent">
    <table id="ctl00_ctl00_ctl00_containerHolder_mainContentHolder_innnerContentHolder_ZakazkaControl_ZakazkaObrazky1_ObrazkyDataList" cellspacing="0" border="0" style="width:100%;border-collapse:collapse;">
    <tr>
        <td align="center" style="width:25%;">
            <div id="ctl00_ctl00_ctl00_containerHolder_mainContentHolder_innnerContentHolder_ZakazkaControl_ZakazkaObrazky1_ObrazkyDataList_ctl02_PictureContainer">
                <a title="1-izb. Kaspická" class="highslide detail-img-link" onclick="return hs.expand(this);" href="/imgcache/cache231/3186-000393~8621457~640x480.jpg"><img src="/imgcache/cache231/3186-000393~8621457~120x120.jpg" class="detail-img" width="89" height="120" alt="1-izb. Kaspická" /></a>
            </div>
        </td><td></td>
    </tr>
</table>
</div>

我以前用过HTMLParser,图片的数量必须添加到“self.srcData".. 先前代码:

^{pr2}$

检查(开始标记)。。这样地?在

if tag == 'div' and len(attrs) > 0 and attrs[0][0] == 'class' and attrs[0][1] == 'detail-headline' \
      and self.srcData[self.getpos()[0]].strip() == 'Fotogal&#233;ria':
      self.status = 3

可以吗?还有。。。?谢谢。在


import urllib
import urllib2
import HTMLParser
import codecs
import time
from BeautifulSoup import BeautifulSoup

# decode string
def decode(istr):
  ostr = u''
  idx = 0
  while idx < len(istr):
    add = True
    if istr[idx] == '&' and len(istr) > idx + 1 and istr[idx + 1] == '#':
      iend = istr.find(';', idx)
      if iend > idx:
        ostr += unichr(int(istr[idx + 2:iend]))
        idx = iend
        add = False
    if add:
      ostr += istr[idx]
    idx += 1
  return ostr

# parser 1
class FlatDetailParser (HTMLParser.HTMLParser):
  def __init__ (self):
    HTMLParser.HTMLParser.__init__(self)

  def loadDetails(self, link):
    self.record = (len(self.characts) + 1) * ['']
    self.status = 0
    self.index = -1
    self.reset()
    request = urllib2.Request(link)
    data = urllib2.urlopen(request)  # URL obtained from the next class
    self.srcData = []
    for line in data:
      line = line.decode('utf8')
      self.srcData.append(line)
    for line in self.srcData:
      self.feed(line)
    self.close()
    return self.record


  def handle_starttag(self, tag, attrs):
    if tag == 'div' and len(attrs) > 1 and attrs[1][0] == 'class' and attrs[1][1] == 'detail-headline' \
      and self.srcData[self.getpos()[0]].strip() == u'Realitn&#225; kancel&#225;ria':
      self.status = 2

    if self.status == 2 and tag == 'div' and len(attrs) > 0 and attrs[0][0] == 'class' \
      and attrs[0][1] == 'name':
      self.record[-1] = decode(self.srcData[self.getpos()[0]].strip())
      self.status = 0

…和下一类解析器,并将数据添加到txt文件中。在

当我使用美妆素时。。什么是汤=Beautiulsoup(???)。如何添加到srcData? 这可以合并吗?怎样?在


Tags: andimportselfdivleniflineattrs
2条回答

如果你使用BeautifulSoup,你的工作会更容易

也许是这样的

from BeautifulSoup import BeaufitulSoup
def count_images(htmltext)
    soup=BeautifulSoup(htmltext)
    return len(soup.findAll('div',{'class':'detail-indent'}))

或使用lxml

^{pr2}$

为了好玩,我尝试了pyparsing方法。Pyparsing包含一些方法来帮助为HTML标记构造匹配模式,这些方法包括属性匹配、意外的空白、单引号或双引号以及其他难以预测的HTML标记陷阱。下面是一个pyparsing解决方案(假设您的HTML源代码已读入字符串变量“HTML”):

from pyparsing import makeHTMLTags

# makeHTMLTags returns patterns for both opening and closing 
# tags, we just want the opening ones
aTag = makeHTMLTags("A")[0]
imgTag = makeHTMLTags("IMG")[0]

# find the matching tags
tagMatches = (aTag|imgTag).searchString(html)

# yes, use len() to see how many there are
print len(tagMatches)

# get the actual image names
for t in tagMatches:
    if t.startA:
        print t.href
    if t.startImg:
        print t.src

印刷品:

^{pr2}$

相关问题 更多 >

    热门问题