python在字符串中查找标记的索引
HTMLpython在字符串中查找标记的索引,python,beautifulsoup,Python,Beautifulsoup,HTML 一条值得你把手弄脏的虫子。Playhut拥有超过六英尺的爬行空间;s Wiggly Worm是一种色彩鲜艳且友好的游戏结构。 6英尺的爬行乐趣 18";易于爬行的直径 色彩鲜艳的设计 产品尺寸:18英寸直径x 60英寸长 建议年龄:3岁及;向上 用于室内使用 代码 def GetBullets(self,Soup): 公告列表=[] 项目符号=str(Soup.findAll('div',{'class':'productDescriptionWrapper'})) 项目符号
一条值得你把手弄脏的虫子。Playhut拥有超过六英尺的爬行空间;s Wiggly Worm是一种色彩鲜艳且友好的游戏结构。
- 6英尺的爬行乐趣
- 18";易于爬行的直径
- 色彩鲜艳的设计
- 产品尺寸:18英寸直径x 60英寸长
- 建议年龄:3岁及;向上
用于室内使用
代码
def GetBullets(self,Soup):
公告列表=[]
项目符号=str(Soup.findAll('div',{'class':'productDescriptionWrapper'}))
项目符号\u re=re.compile(“(.*) ”)
子弹头=str(关于findall(子弹头,子弹头))
索引=项目符号(第页末页(“”)
打印索引
如何提取p
标签和li
标签?谢谢 我们使用它。注意以下几点:
def GetBullets(self, Soup):
bulletList = []
bullets = str(Soup.findAll('div', {'class': 'productDescriptionWrapper'}))
bullets_re = re.compile('<li>(.*)</li>')
bullets_pat = str(re.findall(bullets_re, bullets))
index = bullets_pat.findall('</li>')
print index
>>从BeautifulSoup导入BeautifulSoup
>>>html=“”“”“
>>>Soup=BeautifulSoup(html)
>>>项目符号=Soup.findAll('div',{'class':'productDescriptionWrapper'})
>>>ptags=项目符号[0]。findAll('p')
>>>印刷PTag
[一条值得你把手弄脏的虫子。Playhut的蠕动虫子有超过六英尺的爬行空间,是一种色彩鲜艳、友好的游戏结构。
,用于室内使用
>>>打印ptags[0]。文本
一条值得你把手弄脏的虫子。Playhut拥有超过六英尺的爬行空间;s Wiggly Worm是一种色彩鲜艳且友好的游戏结构。
您可以通过类似的方式查看li标签的内容。小心;这些标记可以相互嵌套,这意味着RE不能正确匹配开始标记和结束标记。这就是为什么您应该使用类似(大概是)BeautifulSoup的解析器。您已经使用BeautifulSoup查找div,然后放弃它,并尝试使用正则表达式解析字符串。我建议你一路上都要坚持使用BeautifulSoup。
def GetBullets(self, Soup):
bulletList = []
bullets = str(Soup.findAll('div', {'class': 'productDescriptionWrapper'}))
bullets_re = re.compile('<li>(.*)</li>')
bullets_pat = str(re.findall(bullets_re, bullets))
index = bullets_pat.findall('</li>')
print index
>>> from BeautifulSoup import BeautifulSoup
>>> html = """ <what you have above> """
>>> Soup = BeautifulSoup(html)
>>> bullets = Soup.findAll('div', {'class': 'productDescriptionWrapper'})
>>> ptags = bullets[0].findAll('p')
>>> print ptags
[<p>A worm worth getting your hands dirty over. With over six feet of crawl space, Playhut’s Wiggly Worm is a brightly colored and friendly play structure.
</p>, <p><strong>Intended for Indoor Use</strong></p>]
>>> print ptags[0].text
A worm worth getting your hands dirty over. With over six feet of crawl space, Playhut’s Wiggly Worm is a brightly colored and friendly play structure.