下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。
内存溢出小编现在分享给大家,也给大家做个参考。
import requestsfrom bs4 import BeautifulSoupimport oss = requests.session()#下载图片def saveimg(imageURL,filename): try: #下载图片设置超时,建立链接3s,下载10s data=requests.get(imageURL,timeout=(3,10)) except: return True f=open(filename,"wb") f.write(data.content) f.close()#建立目录存储每个帖子的图片def mkdir(path): path=path.strip() isExist=os.path.exists(path) if not isExist: os.makedirs(path) return True else: return False#创建目录存储所有帖子mkdir("img")os.chdir("img")#模拟登录,请使用正确的帐户和密码login_data = {'fastloginfIEld': 'username','username': '用户名',"password":"密码","quickforward":"yes","handlekey":"ls"}r=s.post('http://hkbici.com/member.PHP?mod=logging&action=login&loginsubmit=yes&infloat=yes&lssubmit=yes',login_data)#爬第一页的所有帖子,如果爬第二页修改网址forum-18-2r = s.get('http://hkbici.com/forum-18-1.HTML')soup = BeautifulSoup(r.text)href=[]for i in soup.find_all("div",class_="c cl"): href.append(i.find("a").get("href"))dirCount=0for i in href: url="http://hkbici.com/"+i.strip() print(url) sp=BeautifulSoup(s.get(url).text) dirCount+=1 name=str(dirCount)+sp.find("Meta",{"name":"keywords"}).get("content").strip() mkdir(name) count=0 for m in sp.find_all("ignore_Js_op"): count+=1 if m.find("img").get("file"): imageURL="http://hkbici.com/"+m.find("img").get("file").strip() filename =name+"/"+str(count)+".jpg" saveimg(imageURL,filename)
以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
总结以上是内存溢出为你收集整理的python爬比思论坛贴图区图片全部内容,希望文章能够帮你解决python爬比思论坛贴图区图片所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)