wiseyu
一个默默无闻的搬运工

自己使用的是windows系统,所有的介绍都是按照windows进行

一、安装Python和beautifulsoup

  Python下载地址:Python

  beautifulsoup 安装方法:

    下载地址:beautifulsoup 

    解压到python根目录下,然后在控制台使用 pip install beautifulsoup4 进行安装

二、开始写爬虫程序  

 1 from bs4 import BeautifulSoup
 2 from urllib import request
 3 
 4 url="https://www.cnblogs.com/" 
 5 response=request.urlopen(url)
 6 html=response.read();
 7 html=html.decode("utf-8")
 8 bs=BeautifulSoup(html,"html.parser")
 9 for item in bs.find_all("div",class_="post_item"):
10     title=item.find('h3')
11     tuijian=item.find("div",class_="diggit")
12     touxiang=item.find("img",class_="pfs")
13     data=item.find("p",class_='post_item_summary')
14     user=item.find("div",class_="post_item_foot")
15     print("推荐:"+tuijian.text.strip()+"\r\n头像Url:"+touxiang.get('src')+"\r\n标题:"+title.text,"\r\n内容:"+data.text)
View Code

三、最终的结果

 

自己的一小步就是Python学习路上的一大步

posted on 2018-12-12 16:32  wiseyu  阅读(205)  评论(0编辑  收藏  举报

Top