码迷,mamicode.com
首页 > 编程语言 > 详细

Python爬取博客园新闻代码

时间:2017-10-30 14:40:53      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:pen   coding   核心   span   img   bsp   div   write   tps   

核心模块:

requests :安装指令 pip3 install requests
BeautifulSoup :安装指令 pip3 install beautifulsoup4

代码:
import requests
import bs4
import os

response=requests.get(https://news.cnblogs.com/)
response.encoding=response.apparent_encoding

from bs4 import BeautifulSoup
soup=bs4.BeautifulSoup(response.text,features=html.parser)
newslist=soup.find_all(div,class_="content")
#print(newslist)

for new in newslist:
     url=https://news.cnblogs.com+new.a[href]
     print(url+   ,end=‘‘)
     print(new.a.get_text() +    , end=‘‘)
     img=http:+new.div.a.img[src]
     print(img)

     #下载图片
     downloadimg=requests.get(img)
     path=os.getcwd()+/+img.split(/)[-1]
     with open(path,wb) as f:
         f.write(downloadimg.content)

 

 



技术分享

Python爬取博客园新闻代码

标签:pen   coding   核心   span   img   bsp   div   write   tps   

原文地址:http://www.cnblogs.com/Xingsoft-555/p/7753479.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!