Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.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
TypeError:Python中需要字符串或缓冲区_Python_Python 2.7 - Fatal编程技术网

TypeError:Python中需要字符串或缓冲区

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

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('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)
非常感谢您的回复。非常感谢。您建议的代码很有用。非常感谢您的建议。我得到了输出。