爬虫日记之图片爬取和ip查询

图片爬取(单个)

昨天后来又搞了个图片爬取的,先去网上找一张图片的地址下来,然后定义一个url。

import os
import requests  
url=r'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1566405334242&di=6f9b2c49ffec3ebedeaeac8580956b22&imgtype=0&src=http%3A%2F%2Fpic33.nipic.com%2F20131007%2F13639685_123501617185_2.jpg'
root=r'E:\Program Files\feiq\Recv Files\爬虫的内容'     #  :< > / \ | : " * ?;
# path=root +url.split('/')[-1]
path=os.path.join(root,'a.jpg')
print(path)

r=requests.get(url)
if not os.path.exists(root):
        os.mkdir(root)
if not os.path.exists(path):
        r.raise_for_status()
        print(r.raise_for_status())
        with open(path,'wb')as f:
            f.write(r.content)
            print('文件保存成功')
else:
        print('文件已存在')

这里我踩了小坑,拼接起来的存图片路径一直说是非法参数,最后百度才知道文件名不能有 :< > / \ | : " * ?; 这些字符。后来就改成了 a.jpg,果然成功的存储了。但是这就对了后期多图片爬取造成影响了,我每次的图片不就名字都一样了?有个方法可以自动生成不重复的字符串,所以没关系。

这里有一点要注意,图片的存储形式一定是二进制的,所以在前面写入文件的时候是用wb模式而不是wt模式。

网络爬虫与信息提取

img

其实这就是一个关键字爬取,在ip138这个网址来查询输入的ip的所在地。

posted @ 2019-08-22 14:37  chanyuli  阅读(413)  评论(0编辑  收藏  举报