《爬虫系列之爬取PPT》

2023-07-30 13:40:59 浏览数 (1)

这次不多BB,直接上代码系列。

代码语言:javascript复制
import bs4
import requests

headers = {
    "user-agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36 Edg/109.0.1518.70'
}


def level3(URL):
    response = requests.get(URL)
    soup = bs4.BeautifulSoup(response.content, 'lxml')
    title = soup.select('dl>dt>h1>a')[0].get_text()

    zipURL = soup.select('.c1 a')[0]['href']
    zipResponse = requests.get(zipURL)
    file = open('C:\Users\Yezi\Desktop\Python\Crawler\PPT\'   title   '.zip', 'wb')
    file.write(zipResponse.content)
    print(title   ' ^o^ 爬取成功 ^o^')


def level2(URL):
    response = requests.get(URL)
    soup = bs4.BeautifulSoup(response.content, 'lxml')
    level3('https://www.1ppt.com'   soup.select('.downurllist a')[0]['href'])


def level1(URL):
    response = requests.get(URL)
    soup = bs4.BeautifulSoup(response.content, 'lxml')
    ones = soup.select('.tplist h2>a')
    for one in ones:
        try:
            level2('https://www.1ppt.com'   one['href'])
        except:
            print('https://www.1ppt.com'   one['href'])

def pages():
    for page in range(10,186):
        URL='https://www.1ppt.com/xiazai/ppt_xiazai_{}.html'.format(page)
        level1(URL)
        print('^o^ 第{}页爬完 ^o^'.format(page))

pages()

0 人点赞