欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python-python爬取妹子图片

程序员文章站 2023-09-07 16:12:59
1 # -*- conding=utf-8 -*- 2 3 import requests 4 from bs4 import BeautifulSoup 5 import io 6 7 url = "https://www.mzitu.com/164871" 8 #Referer = ? 是模拟电 ......
python-python爬取妹子图片
 1 # -*-  conding=utf-8 -*-
 2 
 3 import  requests
 4 from  bs4 import  beautifulsoup
 5 import io
 6 
 7 url = "https://www.mzitu.com/164871"
 8 #referer = ? 是模拟电脑操作
 9 headers = {"referer":"https://www.mzitu.com/164871","user-agent":"mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, like gecko) chrome/55.0.2883.87 safari/537.36"}
10 html = requests.get(url)
11 soup = beautifulsoup(html.content,'lxml')
12 text = soup.find_all("span")[10].text
13 print(text)
14 title = soup.find("h2",class_='main-title').text
15 #获取图片的地址
16 for i in range(1,10):
17    #herf 为访问的地址
18    herf = url +'/'+ str(i)
19    #在次解析新的url(这个url就是妹子的连接)
20    html = requests.get(herf,headers=headers)
21    beautiful = beautifulsoup(html.text,'lxml')
22    #获取妹子的图片连接
23    pic_url = beautiful.find('img',alt=title)
24    print(pic_url)
25    html = requests.get(pic_url['src'],headers=headers)
26    # print(html.content)
27    # file_name = pic_url['src'].split(r'/')[-1]
28    "
29    # print(file_name)
30 
31    f = open(str(i)+'.jpg','wb')  # 名称
32    f.write(html.content)  #写入图片
33    f.close()
view code