python爬虫的逻辑上的问题,我已经晕了。求高手帮忙

vivre_1 2011-04-11 11:12:51
import urllib
import HTMLParser

class LinkParser(HTMLParser.HTMLParser):
def __init__(self):
HTMLParser.HTMLParser.__init__(self)
self.links= []

def handle_starttag(self,tag,attrs):
if tag == 'a':
for name,value in attrs:
if name == 'href':
self.links.append(value)

def parseLinks(url):
parser = LinkParser()
parser.feed(urllib.urlopen(url).read())
return parser.links

def completeLink(url):
root = "http://127.0.0.1"
if url.startswith('http://'):
return url
elif url.startswith('/'):
return root + url
else:
return root + '/' + url

if __name__=='__main__':
links = parseLinks("http://127.0.0.1")
i = 0
while i < len(links):
links[i] = completeLink(links[i])
try:
morelinks = parseLinks(links[i])
except Exception, e:
print e
j = 0
while j < len(morelinks):
item = completeLink(morelinks[j])
j = j + 1
if item not in links:
print item
links.append(item)
print len(links)
i = i + 1
print links
raw_input()

在以下这个函数里面需要加上一个判断,对于不是我要爬行的URL进行删除,怎么删除啊?可以直接删除main中的links么?
def completeLink(url):
root = "http://127.0.0.1"
if url.startswith('http://'):
return url
elif url.startswith('/'):
return root + url
else:
return root + '/' + url

...全文
175 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
iambic 2011-04-12
  • 打赏
  • 举报
回复
终于换beautiful soap了?
我记得之前也有好几个人问HTMLParser,不知道是不是你,我提过好几次HTMLParser不好用,实践中很少用。
倒不见得是有bug,就是不好用,以致于让你觉得“是HTMLParser的问题”。
vivre_1 2011-04-12
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 iambic 的回复:]

终于换beautiful soap了?
我记得之前也有好几个人问HTMLParser,不知道是不是你,我提过好几次HTMLParser不好用,实践中很少用。
倒不见得是有bug,就是不好用,以致于让你觉得“是HTMLParser的问题”。
[/Quote]

恩 是我啦,当时那段程序写了没用,最近有项目来了,就拿起来改了改,beautiful soup还不错挺好用的,我现在有个问题,您帮我看下吧,抓特定页面的时候 会报错我把报错的代码发给你看下
from BeautifulSoup import BeautifulSoup
import urllib

def completeLink(url):
root = "http://www.chem960.com"
if url.startswith('http://'):
return url
elif url.startswith('/'):
return root + url
else:
return root + '/' + url

if __name__=='__main__':
links = []
try:
soup = BeautifulSoup(urllib.urlopen("http://www.chem960.com/productSupplyShow_582388.shtml").read())
test = soup.findAll('a')
for temp in test:
links.append(completeLink(temp['href']))
print completeLink(temp['href'])
except Exception,e:
print e,22
raw_input()

luotuo512 2011-04-12
  • 打赏
  • 举报
回复
因为有个书上给的爬虫例子是HTMLParser的,哈哈。
[Quote=引用 5 楼 iambic 的回复:]

终于换beautiful soap了?
我记得之前也有好几个人问HTMLParser,不知道是不是你,我提过好几次HTMLParser不好用,实践中很少用。
倒不见得是有bug,就是不好用,以致于让你觉得“是HTMLParser的问题”。
[/Quote]
vivre_1 2011-04-11
  • 打赏
  • 举报
回复
import urllib
import HTMLParser
from BeautifulSoup import BeautifulSoup
import sys
import socket

socket.setdefaulttimeout(2)

class LinkParser(HTMLParser.HTMLParser):
def __init__(self):
HTMLParser.HTMLParser.__init__(self)
self.links= []

def handle_starttag(self,tag,attrs):
if tag == 'a':
for name,value in attrs:
if name == 'href':
self.links.append(value)

def parseLinks(url):
parser = LinkParser()
parser.feed(urllib.urlopen(url).read())
return parser.links

def completeLink(url,domain):
root = domain
if url.startswith('http://'):
return url
elif url.startswith('/'):
return root + url
else:
return root + '/' + url

if __name__=='__main__':
host = sys.argv[1]
links = []
try:
soup = BeautifulSoup(urllib.urlopen(host).read())
test = soup.findAll('a')
for temp in test:
links.append(completeLink(temp['href'],host))
i = 0
while i < len(links):
item=links[i]
if item.startswith(host):
if item.find(":",5)>0:
links.remove(item)
i = i -1
else:
print item
links.remove(item)
i = i - 1
i = i + 1
except Exception,e:
print e
i = 0
while i < len(links):
try:
soup = BeautifulSoup(urllib.urlopen(links[i]).read())
test = soup.findAll('a')
for temp in test:
link = completeLink(temp['href'],host)
if link.startswith(host):
if link not in links:
if link.find(":",5)>0:
print link
else:
print link
links.append(link)
print len(links)
except Exception,e:
print "***********"
print links[i],e
print "***********"
links.remove(links[i])
i = i - 1
i = i + 1
print len(links)
raw_input('end')
vivre_1 2011-04-11
  • 打赏
  • 举报
回复
应该是htmlparser的问题,已经换了beautiful soup 解决了
luotuo512 2011-04-11
  • 打赏
  • 举报
回复
morelinks 中读取的URL不正常,那就打印出来看看啊
vivre_1 2011-04-11
  • 打赏
  • 举报
回复
import urllib
import HTMLParser

class LinkParser(HTMLParser.HTMLParser):
def __init__(self):
HTMLParser.HTMLParser.__init__(self)
self.links= []

def handle_starttag(self,tag,attrs):
if tag == 'a':
for name,value in attrs:
if name == 'href':
self.links.append(value)

def parseLinks(url):
parser = LinkParser()
parser.feed(urllib.urlopen(url).read())
return parser.links

def completeLink(url):
root = "http://www.yxlink.com"
if url.startswith('http://'):
return url
elif url.startswith('/'):
return root + url
else:
return root + '/' + url

if __name__=='__main__':
links = parseLinks("http://www.yxlink.com")
i = 0
while i < len(links):
links[i] = completeLink(links[i])
i=i+1
i = 0
while i < len(links):
item=links[i]
if item.startswith("http://www.yxlink.com"):
print len(links)
else:
print item
links.remove(item)
i = i - 1
i = i + 1
i = 0
while i < len(links):
try:
print urllib.urlopen(links[i]).read()
morelinks = parseLinks(links[i])
except Exception, e:
print e
for item in morelinks:
item = completeLink(item)
print item
if item.startswith("http://www.yxlink.com"):
if item not in links:
links.append(item)
print item
print len(links)
i = i + 1
print "*****************"
for item in links:
print item
print len(links)
raw_input()

上面的问题解决了但是现在
morelinks 中读取的URL不正常,求解释啊。

37,743

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧