[python]抓取网页的内容
2011-10-21 20:17
316 查看
#-*- coding: UTF-8 -*- import urllib2, BeautifulSoup # @param url: complete url # 完整的url # @param usr, pwd: if the page need account, # \p usr and \p pwd will be used # 当访问的页面需要密码的时候会用到 # @return: the formatted string content of the url # 用了BeautifulSoup返回结果文本 def getWebPage(url, usr=None, pwd=None): if not usr and not pwd: content = urllib2.urlopen(url).read() else: pwdMgr = urllib2.HTTPPasswordMgrWithDefaultRealm() pwdMgr.add_password(None, url, usr, pwd) handler = urllib2.HTTPBasicAuthHandler(pwdMgr) opener = urllib2.build_opener(handler) page = opener.open(url).read() content = BeautifulSoup.BeautifulSoup(page).prettify() return content url='http://www.csdn.net/' print getWebPage(url)
相关文章推荐
- python实现抓取网页上的内容并发送到邮箱
- python-爬虫-自带库抓取网页内容
- 零基础写python爬虫之使用urllib2组件抓取网页内容
- Python使用HTMLParser抓取网页内容
- python 网页内容抓取
- python 抓取网页内容教程
- [Python]网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
- paip.抓取网页内容--java php python
- [Python]网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
- Python3学习(34)--简单网页内容抓取(爬虫入门一)
- Python简单实现网页内容抓取功能示例
- paip.抓取网页内容--java php python
- [Python]网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
- [Python]网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
- Chrome + Python 抓取动态网页内容
- python抓取网页内容
- [Python3.x]网络爬虫(一):利用urllib通过指定的URL抓取网页内容
- python抓取网页内容
- 利用python3的urllib.request抓取网页内容并显示中文
- [Python]网络爬虫(二):利用urllib2通过指定的URL抓取网页内容