{"id":13634926,"url":"https://github.com/JackHCC/KeyWord-Crawler","last_synced_at":"2025-04-19T03:34:11.434Z","repository":{"id":112237938,"uuid":"182793805","full_name":"JackHCC/KeyWord-Crawler","owner":"JackHCC","description":"通过输入关键词动态爬取图片保存于本地(Dynamically crawl images by entering keywords and save them locally)","archived":false,"fork":false,"pushed_at":"2019-04-22T13:50:56.000Z","size":8449,"stargazers_count":12,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-08-02T00:21:42.938Z","etag":null,"topics":["crawl","mini-program","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/JackHCC.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-04-22T13:31:38.000Z","updated_at":"2023-12-28T06:41:39.000Z","dependencies_parsed_at":"2023-05-11T21:45:30.048Z","dependency_job_id":null,"html_url":"https://github.com/JackHCC/KeyWord-Crawler","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JackHCC%2FKeyWord-Crawler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JackHCC%2FKeyWord-Crawler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JackHCC%2FKeyWord-Crawler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JackHCC%2FKeyWord-Crawler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JackHCC","download_url":"https://codeload.github.com/JackHCC/KeyWord-Crawler/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223790228,"owners_count":17203350,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["crawl","mini-program","python"],"created_at":"2024-08-02T00:00:37.766Z","updated_at":"2024-11-09T05:30:16.718Z","avatar_url":"https://github.com/JackHCC.png","language":"Python","readme":"# 利用关键词动态爬取想要的图片\n\n### 导入相关库\n主要是requests库\n```\nimport re  # 导入正则表达式模块\nimport requests  # python HTTP客户端 编写爬虫和测试服务器经常用到的模块\nimport random  # 随机生成一个数，范围[0,1]\nimport os #创建路径\n```\n\n### 写爬虫爬取图片函数\n```\ndef spiderPic(html, keyword):       #html：网页；keyword：关键词\n    print('正在查找 ' + keyword + ' 对应的图片,请稍后......')\n    for addr in re.findall('\"objURL\":\"(.*?)\"', html, re.S):  # 动态查找URL\n        print('正在爬取URL地址：' + str(addr)[0:40] + '...')  # 爬取的地址长度超过40时，用'...'代替后面的内容\n\n        try:\n            pics = requests.get(addr, timeout=10)  # 请求URL时间（最大10秒）\n        except requests.exceptions.ConnectionError:\n            print('您当前请求的URL地址出现错误')\n            continue\n\n        fq = open('E:\\\\img\\\\' + (keyword + '_' + str(random.randrange(0, 1000, 4)) + '.jpg'), 'wb')  # 下载图片，并保存和命名\n        fq.write(pics.content)\n        fq.close()\n```\n\n### 主函数\n```\nif __name__ == '__main__':\n    word = input('请输入你要搜索的图片关键字：')\n    result = requests.get(\n        # 通过百度引擎搜索关键词链接\n        'http://image.baidu.com/search/index?tn=baiduimage\u0026ps=1\u0026ct=201326592\u0026lm=-1\u0026cl=2\u0026nc=1\u0026ie=utf-8\u0026word=' + word)\n\n```\n\n### 存放图片文件夹创建\n加入判断是否存在该文件目录\n```\npath='E:\\\\img\\\\';\n# 判断路径是否存在\nisExists = os.path.exists(path)\n\n### 判断结果\nif not isExists:\n    # 如果不存在则创建目录\n    # 创建目录操作函数\n    os.makedirs(path)\n    print\n    path + '创建成功'\n\nelse:\n    # 如果目录存在则不创建，并提示目录已存在\n    print\n    path + ' 目录已存在'\n```\n\n### 调用函数\n```\nspiderPic(result.text, word)\n```\n\n### 数据展示\n我们在输入提示后输入关键词 “风景”并开始爬取图片\n![展示](/1.jpg)\n\n爬取的图片在img文件夹中\n","funding_links":[],"categories":["Uncategorized"],"sub_categories":["Uncategorized"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FJackHCC%2FKeyWord-Crawler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FJackHCC%2FKeyWord-Crawler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FJackHCC%2FKeyWord-Crawler/lists"}