Python-demo(photo)

本文介绍了一个使用Python进行图片批量下载的爬虫实例,通过解析网页源代码,抓取指定网站上清纯唯美主题的图片链接,并利用requests和urllib等库实现图片的自动下载。代码中涉及了UserAgent的随机设置、XPath解析、目录创建和文件保存等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import os
import urllib

import requests
#import wx
import time

from fake_useragent import UserAgent
from lxml import etree
from urllib import request

ua = UserAgent()
headers ={
'user-agent': ua.random
}
opener=urllib.request.build_opener()
opener.addheaders=[('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1941.0 Safari/537.36')]
for i in range(2, 7):
url = "https://www.fb532.com/tupian/list-清纯唯美-%d.html"%i
print(url)

#https://www.fb532.com/tupian/list-%E6%B8%85%E7%BA%AF%E5%94%AF%E7%BE%8E-3.html
#url = "https://www.128nm.com/"
response = requests.get(url,headers = headers)
print(url)
html = response.content.decode("utf-8")
content = etree.HTML(html)

li_list = content.xpath('//ul[@class="clearfix"]/li')
print(li_list)
print(len(li_list))
for li in li_list:
img_name = li.xpath('./a/@title')[0]
print(img_name)
img_url = li.xpath('./a/@data-original')[0]
# print("url:" + img_url)
print(img_name, img_url)
styles = img_url.split(".")
laststyle = styles[len(styles)-1]
print(img_url)
path = "imgs3"
if not os.path.exists(path):
os.mkdir(path)

filename = path + "/" + img_name + "."+laststyle

print(filename)
urllib.request.install_opener(opener)
urllib.request.urlretrieve(img_url, filename)

 

转载于:https://www.cnblogs.com/Chen-MJ/p/11423125.html

0 info it worked if it ends with ok 1 verbose cli [ '/usr/local/bin/node', '/usr/local/bin/npm', 'run', 'serve' ] 2 info using npm@6.14.18 3 info using node@v22.13.0 4 verbose run-script [ 'preserve', 'serve', 'postserve' ] 5 info lifecycle agent-bar-demo@0.1.0~preserve: agent-bar-demo@0.1.0 6 info lifecycle agent-bar-demo@0.1.0~serve: agent-bar-demo@0.1.0 7 verbose lifecycle agent-bar-demo@0.1.0~serve: unsafe-perm in lifecycle true 8 verbose lifecycle agent-bar-demo@0.1.0~serve: PATH: /usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/Users/hyj/Downloads/webrtc-webphone-master/node_modules/.bin:/Library/Frameworks/Python.framework/Versions/3.12/bin:/usr/local/bin:/usr/local/sbin:/Library/Frameworks/Python.framework/Versions/3.9/bin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin://Applications/Topaz Photo AI.app/Contents/Resources/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Users/hyj/Downloads/webrtc-webphone-master/node_modules/.bin 9 verbose lifecycle agent-bar-demo@0.1.0~serve: CWD: /Users/hyj/Downloads/webrtc-webphone-master 10 silly lifecycle agent-bar-demo@0.1.0~serve: Args: [ '-c', 'vue-cli-service serve' ] 11 silly lifecycle agent-bar-demo@0.1.0~serve: Returned: code: 1 signal: null 12 info lifecycle agent-bar-demo@0.1.0~serve: Failed to exec serve script 13 verbose stack Error: agent-bar-demo@0.1.0 serve: `vue-cli-service serve` 13 verbose stack Exit status 1 13 verbose stack at EventEmitter.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:332:16) 13 verbose stack at EventEmitter.emit (node:events:524:28) 13 verbose stack at ChildProcess.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/lib/spawn.js:55:14) 13 verbose stack at ChildProcess.emit (node:events:524:28) 13 verbose stack at maybeClose (node:internal/child_process:1101:16) 13 verbose stack at ChildProcess._handle.onexit (node:internal/child_process:304:5) 14 verbose pkgid agent-bar-demo@0.1.0 15 verbose cwd /Users/hyj/Downloads/webrtc-webphone-master 16 verbose Darwin 22.6.0 17 verbose argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "serve" 18 verbose node v22.13.0 19 verbose npm v6.14.18 20 error code ELIFECYCLE 21 error errno 1 22 error agent-bar-demo@0.1.0 serve: `vue-cli-service serve` 22 error Exit status 1 23 error Failed at the agent-bar-demo@0.1.0 serve script. 23 error This is probably not a problem with npm. There is likely additional logging output above.
03-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值