[原创]python+beautifulsoup爬取整个网站的仓库列表与仓库详情

from bs4 import BeautifulSoup
import requests
import os

def getdepotdetailcontent(title,url):#爬取每个仓库列表的详情
    r=requests.get("https://www.50yc.com"+url).content
    soup = BeautifulSoup(r,"html.parser")
    result = soup.find(name='div',attrs={"class":"sm-content"})#返回元素集
    content = result.find_all("li")#返回元素集
    with open(os.getcwd()+"\\depot\\"+title+"\\depotdetail.txt","w") as f :
        for i in content:
            b = i.find("span").text
            br = i.find("div").text
            f .write(b.replace(" ","").replace("\n","")+br.replace(" ","")+"\n"+"****************************"+"\n")
        f.close()

def getdepot(page):#爬取仓库列表信息
    depotlisthtml = requests.get("https://www.50yc.com/xan"+page).content
    content = BeautifulSoup(depotlisthtml,"html.parser")
    tags = content.find_all(name="div",attrs={"class":"bg-hover"})
    for i in tags:
        y = i.find_all(name="img")#返回tag标签
        for m in y:
            if m["src"].startswith("http"):
                imgurl = m["src"]
        print(imgurl)
        title = i.strong.text
        depotdetailurl = i.a['href']
        # print(depotdetailurl)
        os.mkdir(os.getcwd()+'\\depot\\'+title+'\\')
        with open(os.getcwd()+'\\depot\\'+title+'\\'+"depot.jpg","wb") as d :
            d.write(requests.get(imgurl).content)
        with open(os.getcwd()+'\\depot\\'+title+'\\'+"depot.txt","w") as m:
            m.write(i.text.replace(" ",""))
            m.close()
        getdepotdetailcontent(title,depotdetailurl)


for i in range(1,26):#爬取每页的仓库列表与仓库详情
    getdepot("/page"+str(i))
    print("/page"+str(i))

爬取内容为:

 

 爬取结果如下:

 

转载于:https://www.cnblogs.com/lelexiong/p/10880378.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值