Python 普通爬虫(爬图片,只支持一页)


import urllib.request
import re
import os

def imageCrawler(url,toPath):
headers = {
"User-Agnet": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)Chrome/56.0.2924.90 Safari/537.36 2345Explorer/9.6.0.18627"}
req=urllib.request.Request(url,headers=headers)
response=urllib.request.urlopen(req)
htmlStr=response.read().decode("utf-8")
# with open(r"D:\学习\Python\pycharm\234\爬虫\爬图片\yhd.html","wb") as f:
#f.write(htmlStr)
pat=r'<img original="//(.*?)" '
re_image=re.compile(pat ,re.S)
imagesList=re_image.findall(htmlStr)
#print(imagesList)
print(len(imagesList))
#print(imagesList[0])
num=1
for imageUrl in imagesList:
path=os.path.join(toPath,str(num)+".jpg")
num+=1
#把图片下载到本地
urllib.request.urlretrieve("http://"+imageUrl,filename=path)

url="https://search.yhd.com/c0-0/k%25E6%2597%25B6%25E5%25B0%259A%25E8%25A3%2599%25E8%25A3%2585/"
toPath=r"D:\image"
imageCrawler(url,toPath)
posted @ 2019-03-14 13:57  飞飞阿  阅读(420)  评论(0编辑  收藏  举报