加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python爬比思论坛贴图区图片

发布时间:2020-12-17 17:04:10 所属栏目:Python 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 import requestsfrom bs4 import BeautifulSoupimport oss = requests.session()#下载图片def saveImg(imageURL,fileName): try: #下载图片设置超时,

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

import requests
from bs4 import BeautifulSoup
import os

s = 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-2
r = 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=0
for 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)





以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读