1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
#coding:utf-8 from hashlib import md5 import requests,os ''' format 字符串格式化 os.getcwd() 获取当前的目录 md5(content).hexdigest() 生成md5 os.path.exists检测文件是否存在 ''' def save_image(content): file_path = '{0}/{1}.{2}'.format(os.getcwd(), md5(content).hexdigest(), 'jpg') print(file_path) if not os.path.exists(file_path): with open(file_path, 'wb') as f: f.write(content) #二进制写入 f.close() else: print("已存在") def main(url): res = requests.get(url) if res.status_code==200: content=res.content save_image(content) if __name__ == '__main__': url="https://ss0.bdstatic.com/k4oZeXSm1A5BphGlnYG/skin/767.jpg" main(url) #=================if else 单行判断========================== title = result if result else 'None' #如果存在 则返回把result赋值给title 如果不存在则返回None #dict loops for (k,v) in dict.items(): print "dict[%s]=" % k,v # Json.dumps默认输出是的ascii码,转成中文 def write_to_file(content): with open('result.txt', 'a', encoding='utf-8') as f: # utf-8的编码打开 f.write(json.dumps(content, ensure_ascii=False) + '\n') #取消 对ascii的支持 # requests 的常用的异常处理方法 from requests.exceptions import RequestException def get_one_page(url): try: response = requests.get(url) if response.status_code == 200: #判断状态码 return response.text return None except RequestException: #捕获异常 return None |
selenium 显示等待和隐式等待很重要
http://www.168seo.cn/python/23837.html
