Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 解析时获取元素标记内的内容_Python_Selenium_Beautifulsoup - Fatal编程技术网

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.