Python简单的爬虫:Google的全部doodles

刚接触Python,看了一点点《Python网络数据采集》。开始主要是关于BeautifulSoup4的介绍。于是用下现在的知识,写了一个爬所有Google的Doodles图片的脚本,初学者,各位大神多多关照。顺便把爬到的东西也贴出来吧。这东西在自己VPS上爬的,国内需梯子,而且我发现在自己的win上爬的超慢,不知道是不是完全是网络的问题。
直接贴代码:(BeautifulSoup4需要自己pip,测试版本为py3.5.1)

from urllib.request import urlopen
from bs4 import BeautifulSoup
import re
import datetime
import random

def getLinks(doodlesTitle):
    html=urlopen("https://www.google.com"+doodlesTitle)
    bsObj=BeautifulSoup(html)
    title=bsObj.find("h2").get_text()
    picUrl=bsObj.find("img",{"id":"hplogo-img"}).attrs['src']
    PicDate=bsObj.find("div",{"class":"time"}).get_text()
    downloadPic('https:'+picUrl)
    return bsObj.find("a",{"title":"Prev"}).attrs['href']

def downloadPic(picUrl):
    listChar=picUrl.split('/')
    picName=listChar[-1]#沿用网站上的文件名
    pic=urlopen(picUrl)
    f=open(picName,"wb")
    f.write(pic.read())
    f.close()

count=1000 #其实目前为止只有650
links=getLinks("/doodles/100th-anniversary-of-the-russian-nature-reserves") #2017/1/12
while count>0:
    links=getLinks(links)
    count=count-1;

代码不堪入目,请见谅。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值