为什么当我使用 beautifulsoup 抓取 HTML 中的字符串时它们不起作用
Why they didn't work when I scrape the string in HTML by using beautifulsoup
我想使用 beautifulsoup 工具在 HTML 中抓取倾斜字符串 "The Dormouse's story"。
#!/usr/bin/python
# -*- coding: utf-8 -*-
from bs4 import BeautifulSoup
html_doc = """
<html>
<head>
<title>The Dormouse's story</title>
</head>
<body>
<p class="title"><b>The Dormouse's story</b>
</p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>; and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""
soup = BeautifulSoup(html_doc)
print soup.title.string
我用的是Python2.7.9 IDE,错误描述为
Traceback (most recent call last):
File "C:/Python27/Scripts/test.py", line 23, in <module>
print soup.title.string
File "C:\Python27\lib\idlelib\PyShell.py", line 1353, in write
s = unicode.__getslice__(s, None, None)
TypeError: an integer is required
如何解决这个问题,是否需要更改变量类型?
我认为这是你 IDE 中的一个错误,当我使用它时,没有错误。
<<ret = urllib2.urlopen(url).read()
<<soup = BeautifulSoup(ret)
<<print soup.title.string
<<About Me
当您使用 运行 模块 命令 运行 脚本时,这是 IDLE 中的一个错误;解决方法是在对象上使用 unicode()
:
print unicode(soup.title.string)
此问题是由 issue 19481 的错误修复引起的;我怀疑 unicode.__getslice__
方法得到了清理和/或从未支持使用 None
作为索引,至少在 Python 2.
我已经为这个问题打开了 new bug on IDLE。
我想使用 beautifulsoup 工具在 HTML 中抓取倾斜字符串 "The Dormouse's story"。
#!/usr/bin/python
# -*- coding: utf-8 -*-
from bs4 import BeautifulSoup
html_doc = """
<html>
<head>
<title>The Dormouse's story</title>
</head>
<body>
<p class="title"><b>The Dormouse's story</b>
</p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>; and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""
soup = BeautifulSoup(html_doc)
print soup.title.string
我用的是Python2.7.9 IDE,错误描述为
Traceback (most recent call last):
File "C:/Python27/Scripts/test.py", line 23, in <module>
print soup.title.string
File "C:\Python27\lib\idlelib\PyShell.py", line 1353, in write
s = unicode.__getslice__(s, None, None)
TypeError: an integer is required
如何解决这个问题,是否需要更改变量类型?
我认为这是你 IDE 中的一个错误,当我使用它时,没有错误。
<<ret = urllib2.urlopen(url).read()
<<soup = BeautifulSoup(ret)
<<print soup.title.string
<<About Me
当您使用 运行 模块 命令 运行 脚本时,这是 IDLE 中的一个错误;解决方法是在对象上使用 unicode()
:
print unicode(soup.title.string)
此问题是由 issue 19481 的错误修复引起的;我怀疑 unicode.__getslice__
方法得到了清理和/或从未支持使用 None
作为索引,至少在 Python 2.
我已经为这个问题打开了 new bug on IDLE。