Python练手项目0008

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


问题描述:练习0008的问题是你有一个网页,需要将网页的正文提取出来。具体代码如下:

# -*- coding: utf-8 -*-
"""
Created on Mon Jan 09 13:10:54 2017


@author: sky
"""


import requests
from bs4 import BeautifulSoup
import codecs


url='http://www.baidu.com'
html=requests.get(url)


soup=BeautifulSoup(html.text)
a = soup.body.text.encode('GBK','ignore').decode('GBK')
b = a.encode('utf-8')
file = codecs.open('1.txt','w')
file.write(b)
file.close()


print a


注意:利用beautifulsoup可以进行简单的文字提取

但是提取出的结果为Unicode,需要用codercs进行转换

详细代码和结果,可以参考https://github.com/g8015108/exercise-for-python

Unicode可以参考http://www.cnblogs.com/jackge/archive/2013/06/04/3117352.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值