Beautifulsoup的用法實(shí)例

Beautifulsoup主要的功能是從網(wǎng)頁(yè)抓取數(shù)據(jù),相對(duì)于正則表達(dá)式來(lái)說(shuō),更簡(jiǎn)便,具體實(shí)例如下:

本文的主要目的是從賽希網(wǎng)下載歷年試題,使用Beautifulsoup之前一定要分析網(wǎng)頁(yè)結(jié)構(gòu),soup.select和soup.find_all返回的都是列表結(jié)構(gòu),想要從列表中獲取相應(yīng)的數(shù)據(jù),需要遍歷列表。此處獲取URL僅僅用一句話URL = soup.find_all(href=re.compile("pdf"))就能夠?qū)崿F(xiàn)。非常簡(jiǎn)便。

再標(biāo)記一下enumerate()函數(shù),函數(shù)傳入的是一個(gè)序列、迭代器或其他支持迭代對(duì)象,返回的是 enumerate(枚舉) 對(duì)象。

# -*- coding:utf-8 -*-
import re
import sys
import requests
from bs4 import BeautifulSoup

def Download_URL_List():
    url = 'http://www.educity.cn/rk/zhenti/test/'
    user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36'
    headers = { 'User-Agent' : user_agent}
    try:
        html = requests.get(url,headers=headers)#連接
    except Exception as e:
        print(e)
    content = html.text#獲取內(nèi)容,自動(dòng)轉(zhuǎn)碼unicode
    soup = BeautifulSoup(content,"lxml")
    URL = soup.find_all(href=re.compile("pdf"))
    return URL

def Download_File(URL):
    pattern = re.compile(r'[a-zA-z]+://[^\s]*.pdf')
    for i,pdf_url in enumerate(URL):
        download_url = ((pattern.search(str(pdf_url))).group(0))
        req = requests.get(download_url)
        string = str(i+1)+'.pdf'
        with open(string,'wb') as pdf:
            pdf.write(req.content)
      
if __name__ == '__main__':
    Download_File(Download_URL_List())
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容