python下载我要个性网图片
本帖最后由 仿佛_一念成佛 于 2018-6-23 07:11 编辑import requests
import lxml
from bs4 import BeautifulSoup
import os
import urllib.request
def get_html(url):
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.117 Safari/537.36'}
try:
html = requests.get(url, headers=headers)
html.raise_for_status()
html.encoding = 'utf-8'
return html.text
except:
print('something is wrong')
def get_url(url):
html = get_html(url)
soup = BeautifulSoup(html, 'lxml')
urls = soup.find_all('div',class_="txList ")
for i in urls:
href = i.a['href']
#print(href)
urls = 'https://www.woyaogexing.com'
baseurl = urls + href
#print(baseurl)
#return baseurl
html_img = requests.get(baseurl)
html_img.encoding = 'utf-8'
#print(html_img.text)
soup_src = BeautifulSoup(html_img.text, 'lxml')
src = soup_src.find_all('li', class_="tx-img")
for a in src:
li = a.a['href']
base = 'https:' + li
#print(base)
ref = urllib.request.Request(base)
ref.add_header('user-agent', 'Mozilla/5.0')
buf = urllib.request.urlopen(ref).read()
filename = 'D:/1234/girl/' + base.split('/')[-1]
with open(filename, 'wb') as file:
file.write(buf)
print('成功保存了!')
if __name__ == '__main__':
url ='https://www.woyaogexing.com/touxiang/weixin/'#这里可改其他我要个性网的网址,翻页的话加个for循环即可,这里我就不加了,懒
get_url(url)
我只是小白,如果哪个地方还能改的话请大佬指出来,共同学习 代码用代码框处理下吧
【公告】发帖代码插入教程
https://www.52pojie.cn/thread-713042-1-1.html
(出处: 吾爱破解论坛)
苏紫方璇 发表于 2018-6-22 08:09
代码用代码框处理下吧
【公告】发帖代码插入教程
https://www.52pojie.cn/thread-713042-1-1.html
这个大神,我好像是第一次看到啊,名字很中国风 我是来抢个沙发 共同学习 python比java好多了,只需简单的代码就能实现目标。 顶楼主多谢分享 胆肥啊,要性网图片呢?哪有? 感谢楼主分享
感谢楼主分享 这个表示没有用过,但其他人,估计可以借鉴。