您的位置:首页 > 编程语言 > Python开发

Python练手项目0009

2017-01-10 13:25 267 查看
本项目采用的是https://github.com/Yixiaohan/show-me-the-code中所提供的练习项目,所有代码均为原创,转载请注明,谢谢。

问题描述:给出一个html文件,找出里面的链接,其具体代码如下:

# -*- coding: utf-8 -*-

"""

Created on Tue Jan 10 13:18:55 2017

@author: sky

"""

import urllib2

from bs4 import BeautifulSoup

url = "http://www.baidu.com"

page = urllib2.urlopen(url)

file = open('result.txt','w')

soup = BeautifulSoup(page)

links = soup.findAll('a')

for link in links:

    a = link['href']

    file.write(str(a))

    print(link['href'])

file.close()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python