Python 从第一个标记抓取一个简单字符串
老实说,我觉得Python 从第一个标记抓取一个简单字符串,python,beautifulsoup,Python,Beautifulsoup,老实说,我觉得BeautifulSoup太难了,文档中没有解释我要寻找的基础知识 我试图返回具有属性的标记内的字符串: <span class="on">6220</span> 给我[6220]。所以我觉得我做得都不对,从标记中提取一个简单字符串的方法是什么?你可以这样做: html = # your HTML source goes here soup = BeautifulSoup(html) x = soup.find('span', {'class' : 'on
BeautifulSoup
太难了,文档中没有解释我要寻找的基础知识
我试图返回具有属性的标记内的字符串:
<span class="on">6220</span>
给我
[6220]
。所以我觉得我做得都不对,从标记中提取一个简单字符串的方法是什么?你可以这样做:
html = # your HTML source goes here
soup = BeautifulSoup(html)
x = soup.find('span', {'class' : 'on'})
print x.text
print x.string
print x.contents[0]
替换
return str(users)
与
或
页面('span…
调用实际上是调用find_all()
函数的简写符号,该函数返回一个列表。因此,您首先索引该列表,获取标记,然后获取其内容。
。运行Pythonstr()
上的函数将为您提供全部功能-您需要使用BeautifulSoup函数来获取标记字符串。确实,BeautifulSoup不是那么容易理解,但有时可能非常有用;)
因此,重新以FlopCoder为例,对其进行进一步解释:
html = # HTML Code #maybe parsed from a website
soup = BeautifulSoup(html) #you create a soup object with your html code
x = soup.find('span', {'class' : 'on'}) #Search for the first span balise in the code, whith class : on
print x.text #Find the found balise, .text mean only the text inside the <>text</>
最后一个示例使用findAll。它创建了一个列表,其中包含代码中Class:On的所有span应答器。这样你就可以运行一个
您的_object.text-->返回文本
您的_对象.a-->返回链接(等等…)
希望能对你有所帮助
return users[0].string
return users[0].contents
html = # HTML Code #maybe parsed from a website
soup = BeautifulSoup(html) #you create a soup object with your html code
x = soup.find('span', {'class' : 'on'}) #Search for the first span balise in the code, whith class : on
print x.text #Find the found balise, .text mean only the text inside the <>text</>
x = soup.findAll('span', {'class' : 'on'})
for span in x:
print span.text