Python 解析时获取元素标记内的内容
鉴于我的代码:Python 解析时获取元素标记内的内容,python,selenium,beautifulsoup,Python,Selenium,Beautifulsoup,鉴于我的代码: browser.get(s_page_url) soup = BeautifulSoup(browser.page_source, "html.parser") s_image_element = soup.find('a', {'id': 'angle-3'}) s_image_href = s_image_element['href'] s_image_url = "http://www.zappos.com" + s_image_href s_title_element =
browser.get(s_page_url)
soup = BeautifulSoup(browser.page_source, "html.parser")
s_image_element = soup.find('a', {'id': 'angle-3'})
s_image_href = s_image_element['href']
s_image_url = "http://www.zappos.com" + s_image_href
s_title_element = soup.find('h1', {'class': 'banner'})
print s_title_element
当前生成以下输出:
<h1 class="banner">
<a href="http://couture.zappos.com/a-testoni">a. testoni</a>
<meta content="a. testoni" itemtype="brand">
<a href="/p/a-testoni-sport-nappa-calf-sneaker/product/8835012"><span class="ProductName" itemprop="name">Sport Nappa Calf Sneaker</span></a>
</meta></h1>
但会收到以下错误消息:
File "C:\Python27\lib\site-packages\bs4\element.py", line 958, in __getitem__
return self.attrs[key]
KeyError: 'a'
get_text
将连接标记对象中的所有文本strip=True
将删除开头和结尾的空白
def get_text(self, separator="", strip=False,
types=(NavigableString, CData)):
"""
Get all child strings, concatenated using the given separator.
谢谢,我如何在单词之间留出一个空格,因为我得到的是
a。testoniSport Nappa Calf运动鞋moment@methuselah这是源代码,重要的是参数。
print s_title_element.get_text(strip=True,separator=" " )
def get_text(self, separator="", strip=False,
types=(NavigableString, CData)):
"""
Get all child strings, concatenated using the given separator.