新手--某美女图片爬取
代码写的很糙,希望各位大佬莫笑,自学了一段时间,还在学习的过程中昨天学了bs4,就想哪来练练手,于是有了今天的这段代码,希望大佬指正一下,代码不通顺需要提高的地方,并注释一下,将不胜感激!!!
代码如下:# coding:utf-8 学好python,天天向上
import requests
from bs4 import BeautifulSoup
import time
ms=int(input('请输入爬取内容:1、性感美女 2、清纯可爱 3、性感御姐 4、制服诱惑'))
page=1
url1="https://dimgw.us/xinggan/page/"+str(page) # 性感美女
url2="https://dimgw.us/qc/page/"+str(page) # 清纯可爱
url3="https://dimgw.us/yj/page/"+str(page) # 性感御姐
url4="https://dimgw.us/zf/page/"+str(page) # 制服诱惑
if ms==1:
url=url1
elif ms==2:
url=url2
elif ms==3:
url=url3
elif ms==4:
url=url4
else:
print('输入有误,请重新输入')
headers={
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36",
"Referer":"https://dimgw.us/zf"
}
domain_resp=requests.get(url,headers=headers)
domain_page=BeautifulSoup(domain_resp.text,'html.parser')
# print(domain_page)
a_tag=domain_page.find_all('h2',class_="entry-title") # 拿到想要的标签内容
# print(a_tag)
while True:
for child_href in a_tag:
b_tag=child_href.find_all('a')
# print(b_tag)
c_tag=str(b_tag) # 对内容进行切割,获取网址
# href=c_tag.get('href')
# print(c_tag)
child_resp=requests.get(c_tag,headers=headers)
child_page=BeautifulSoup(child_resp.text,'html.parser')
# print(child_page)
# 未完成,子页面内图片跳转到另外网站
div=child_page.find('div',class_="entry-content u-text-format u-clearfix").find_all('img')# 获取子页面内img标签内容
# print(div)
for it in div:
src=it.get('src') # 拿到页面内的图片下载地址
img_name= src.split('/')[-1]
# print(src)
img=requests.get(src,headers=headers)
img.content
with open(r'练习小说/'+img_name,'wb') as f: # 报错:OSError: Invalid argument:
f.write(img.content)
print('完成',img_name)
time.sleep(1)
f.close()
page+=1
print('下载完成')
先收藏了,万一用到呢 谢谢@Thanks!
感谢楼主的分享 谢谢大佬 尊重原创者,更尊重搬运者,辛苦了 谢谢大牛
页:
[1]