文章标题

来源:互联网 发布:手机淘宝评价删除不掉 编辑:程序博客网 时间:2024/06/07 08:29

Requests+BeautifulSoup实现爬虫

没有requests库的可采用pip install requests进行安装  此处所采用的python版本为Python3.5

import requestsfrom bs4 import BeautifulSoupglobal urldef getshouye():    html=requests.get("http://www.jianshu.com/")    #print('html.text')    content=html.text    bsobj=BeautifulSoup(content,"lxml")    namelist=bsobj.find_all("a",class_='blue-link')    for name in namelist:        print(name.text)        url=name['href']        #getwriter(url)        writer_html=requests.get("http://www.jianshu.com/"+url)        writer_cont=writer_html.text        writer_bs=BeautifulSoup(writer_cont,'lxml')        writer_info=writer_bs.find_all("div",class_='meta-block')        for info in writer_info:            #info=            if info.a is not None:                flowing=info.a.p.text                print("%s" %(info.a.i.previous_element))            else:                print("%s: % s" % (info.div.text,info.p.text))        #passgetshouye()

”’

这里写图片描述

此处代码有有点问题,应该是会打印出关注,粉丝,文章的个数,有兴趣的小伙伴们可以对代码进行修改。当然,作为一个学了几天python的我写出的这个爬虫有很多问题,所以还是沉下心来继续学习基础知识。真的不建议大家着急,一上来就想弄这弄那的,还是要打好基础。
对于才代码中的一些问题可以自行百度,或者留言。

哈哈,搬砖去啦,大家加油。