py课的作业爬妹子图有借鉴的可以下面回复
import urllib.requestpicsite = []
url = "http://jandan.net/girl/"
i = int(input("输入要爬行的页数:"))
def openurl(url):
req = urllib.request.Request(url)
req.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36')
response = urllib.request.urlopen(req)
html = response.read().decode('utf-8')
return html
#找图片地址
def find_picture(url):
a = b =0
html = openurl(url)
while not(a == 8):
a = html.find("img src=", b) + 9
b = html.find('"', a)
if a != 8:
picsite.append("http:" + html)
#找下一页的地址
def openurl_next(url):
html = openurl(url)
a = html.find("Older Comments") + 22
b = html.find('"', a)
url = "http:" + html
return url
#下载图片
def down_picture():
for i in range(len(picsite)):
url = picsite
downpicture = urllib.request.urlopen(url)
apt = downpicture.read()
with open(str(i) + '.jpg', "wb") as f:
f.write(apt)
#开始运行程序
def start(i):
aurl = url
while i:
find_picture(aurl)
next_url = openurl_next(aurl)
aurl = next_url
i -= 1
down_picture()
if __name__ == "__main__":
start(i)
页:
[1]