我需要计算图像的数量(在这种情况下是1张图像)。显然使用“len()”?
这是HTML:
<div class="detail-headline">
Fotogalé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”中。 上一个代码:
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á kancelá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
然后(检查开始标记)..喜欢这个?
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é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á kancelá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文件中。
当我使用BeautifulSoup时..什么是汤= BeautifulSoup(???)。如何添加到srcData? 这可以合并吗?怎么样?
答案 0 :(得分:3)
如果您使用BeautifulSoup
,您的工作会更轻松也许是这样的
from BeautifulSoup import BeaufitulSoup
def count_images(htmltext)
soup=BeautifulSoup(htmltext)
return len(soup.findAll('div',{'class':'detail-indent'}))
或使用 lxml
from lxml.html.soupparser import fromstring
def count_images(htmltext)
return len([e.attrib for e in fromstring(htmltext).findall('div')
if e.attrib.get('class')=='detail-indent'])
答案 1 :(得分:1)
只是为了百灵鸟,我尝试了一种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
打印:
2
/imgcache/cache231/3186-000393~8621457~640x480.jpg
/imgcache/cache231/3186-000393~8621457~120x120.jpg