TypeError:Python中需要字符串或缓冲区
TypeError:应为字符串或缓冲区TypeError:Python中需要字符串或缓冲区,python,python-2.7,Python,Python 2.7,TypeError:应为字符串或缓冲区 from BeautifulSoup import BeautifulSoup import urllib2 import re html_page = urllib2.urlopen("http://kteq.in/services") soup = BeautifulSoup(html_page) for link in soup.findAll('a'): result = re.sub(r"http\S+", "", link.get('hr
from BeautifulSoup import BeautifulSoup
import urllib2
import re
html_page = urllib2.urlopen("http://kteq.in/services")
soup = BeautifulSoup(html_page)
for link in soup.findAll('a'):
result = re.sub(r"http\S+", "", link.get('href'))
print result
print "____________________________________________________"
当我运行上述代码时,它在第7行显示TypeError。无法纠正错误。请建议我。尝试打印href值
for link in soup.findAll('a'):
print(link.get('href'))
result = re.sub(r"http\S+", "", link.get('href'))
您将看到在提取几个链接后出现一个None
值
您可以通过在循环中提供if条件来解决此问题
for link in soup.findAll('a'):
print(link.get('href'))
if link.get('href')==None:
continue
result = re.sub(r"http\S+", "", link.get('href'))
print link.get('href')
,您将知道错误,您可以在beautiful-soup
中尝试传递解析器,就像BeautifulSoup(html\u页面,html.parser)
非常感谢您的回复。非常感谢。您建议的代码很有用。非常感谢您的建议。我得到了输出。