学会了怎么使用os模块

#!/usr/bin/python
# -*- encoding:utf-8 -*- import requests
import os
from bs4 import BeautifulSoup def urlBS(url):
response = requests.get(url)
# print response.encoding #查看request解析的网页的编码
response.encoding = 'utf-8' #requests自动识别的编码有误,强制更改编码方式
soup = BeautifulSoup(response.text,'lxml') #前面用的soup = BeautifulSoup(response)一直报错,后面参考用etree的方式,我加了个.text搞定
# print soup.original_encoding #查看BeautifulSoup解析的网页的编码
return soup def get_article(url):
#得到文档的链接
booklist_soup=urlBS(url)
herf_list=booklist_soup.select('.booklist a') path = os.getcwd()+u'/读者文章保存'+time+u'/' #取得当前目录
if not os.path.isdir(path): #如果当前目录不存在读者文章保存这个文件夹,则新建它
os.mkdir(path) for herf in herf_list:
newurl=baseurl+herf['href']
# print newurl
result=urlBS(newurl) title=result.find("h1").string #获得文章标题
filename = path + title + '.txt'
#print filename
author=result.find(id='pub_date').string.strip() #获得作者,strip去除前后空格
print filename+' '+author
#写入文档
new=open(filename,"w")
new.write("<<" + title.encode("utf-8") + ">>\n\n")
new.write(author.encode("utf-8")+"\n\n")
#文章信息
text=result.select(".blkContainerSblkCon p")
# file=open('testfile.text','w')
for p in text:
# print p.text
#content=p.string.strip() #用strip导致不会换行了,写进去后所有的资料都变成了一行
content=p.text #直接用.text原文的换行都会存在,保存较完好
new.write(content.encode("utf-8"))
new.close()

最新文章

  1. C#中使用OpenSSL的公钥加密/私钥解密
  2. AWS 搭建 VPN 服务(PPTP &amp; L2TP) 整理
  3. SQL Developer 4.0 启动报错“unable to create an instance of the java virtual machine located at path”
  4. read the python code and predict the results --- from &lt;Learn Python The Hard Way&gt;
  5. MySQL 设置允许远程登录
  6. 【Qt】QDialog之屏蔽Esc键【转】
  7. asp.net多图片上传实现程序代码
  8. 解决fontawesome-webfont 被拦截的问题
  9. 移动开发:初学 iOS-UIViewController 心得
  10. 如何设置SVN提交时强制添加注释
  11. Android_Studio常用插件
  12. 纯CSS3实现的图片滑块程序 效果非常酷
  13. linux_安装 redis
  14. 你不知道你不懂javascript
  15. Python中使用面状矢量裁剪栅格影像,并依据Value值更改矢量属性
  16. 12.scrapy框架之递归解析和post请求
  17. Yii2的save()方法容易出错的地方
  18. rt-thread中线程内置定时器的作用 ---
  19. &lt;TCP/IP&gt;DHCP动态主机配置协议
  20. 18.翻译系列:EF 6 Code-First 中的Seed Data(种子数据或原始测试数据)【EF 6 Code-First系列】

热门文章

  1. 05《UML大战需求分析》之五
  2. java连接AD域
  3. 文件类型总结 MIME
  4. MongoDB_聚合
  5. IT级别
  6. web前端学习基础知识(1)
  7. hibernate增删改查总结
  8. bzoj 1293: [SCOI2009]生日礼物 问题转化 + 性质分析 + 滚动数组优化
  9. Linux的mysql搭建
  10. vue 动态添加路由 require.context()