我正在尝试用python2.7中的漂亮汤构建一个基本的web爬虫。这是我的代码:
import re
import httplib
import urllib2
from urlparse import urlparse
from bs4 import BeautifulSoup
regex = re.compile(
r'^(?:http|https)s?://' # http:// or https://
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' #domain...
r'localhost|' #localhost...
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
r'(?::\d+)?' # optional port
r'(?:/?|[/?]\S+)$', re.IGNORECASE)
def isValidUrl(url):
if regex.match(url) is not None:
return True;
return False
def crawler(SeedUrl):
tocrawl=[SeedUrl]
crawled=[]
while tocrawl:
page=tocrawl.pop()
print 'Crawled:'+page
pagesource=urllib2.urlopen(page)
s=pagesource.read()
soup=BeautifulSoup.BeautifulSoup(s)
links=soup.findAll('a',href=True)
if page not in crawled:
for l in links:
if isValidUrl(l['href']):
tocrawl.append(l['href'])
crawled.append(page)
return crawled
crawler('https://www.google.co.in/?gfe_rd=cr&ei=SfWxVs65JK_v8we9zrj4AQ&gws_rd=ssl')
我发现了一个错误:
爬行:rd=ssl跟踪(最近一次调用):文件"web_crawler_python_2.py",第38行,在爬虫(‘rd=ssl’)文件“web_crawler_python_2.py”中,第29行,在爬虫soup=BeautifulSoup.BeautifulSoup(s) AttributeError中:输入object 'BeautifulSoup‘没有属性'BeautifulSoup’
我试了很多次,但似乎无法调试它。谁能告诉我这个问题。(顺便提一句,我知道很多网站都不允许爬行,但我只是为了学习。)
谢谢,任何帮助都将不胜感激。
代码使用的源:简易网络爬虫
发布于 2016-02-03 12:55:15
这个类没有属性BeautifulSoup。我不知道你为什么用它。来自文档的示例
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc, 'html.parser')
你需要更换:
BeautifulSoup.BeautifulSoup
至
BeautifulSoup
https://stackoverflow.com/questions/35177635
复制相似问题