首页 > Web开发 > 详细

BeautifulSoup 抓取网站url

时间:2017-10-08 16:05:49      阅读:291      评论:0      收藏:0      [点我收藏+]
  1 # -*- coding:utf-8 -*-
  2 import urlparse
  3 import urllib2
  4 from bs4 import BeautifulSoup
  5 
  6 url = "http://www.baidu.com"
  7 
  8 urls = [url] # stack of urls to scrape
  9 visited = [url] # historic record of urls
 10 
  1 # -*- coding:utf-8 -*-
  2 import urlparse
  3 import urllib2
  4 from bs4 import BeautifulSoup
  5 
  6 url = "http://www.baidu.com"
  7 
  8 urls = [url] # stack of urls to scrape
  9 visited = [url] # historic record of urls
 10 
 11 while len(urls) > 0:
 12     try:
 13         htmltext = urllib2.urlopen(urls[0]).read()
 14     except:
 15         print urls[0]
 16     soup = BeautifulSoup(htmltext,"html")
 17 
 18     urls.pop(0)
 19 
 20     for tag in soup.findAll("a", href=True):
 21         tag["href"] = urlparse.urljoin(url, tag["href"])
 22         if url in tag["href"] and tag["href"] not in visited:
 23             urls.append(tag["href"])
 24             visited.append(tag["href"])
 25 
 26     print len(urls)

 

BeautifulSoup 抓取网站url

原文:http://www.cnblogs.com/cuzz/p/BeautifulSoup.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!