2023年5月14日 星期日

爬蟲筆記-除了Requests庫外還有Requests-HTML


Requests-HTML

他是在原有Requests庫的基礎上進一步封裝而成的HTML解析庫
以下是他的優缺

  1. HTML解析:內痔多種解析HTML的方式,包括XPath和CSS Selector
  2. JavaScript渲染:Requests-HTML使用了基於Pyppeteer的異步JavaScrip渲染引擎,
    因此可以渲染JavaScript載入的頁面。
  3. 更豐富的API:Requests-HTML提供了一些特殊的方法和屬性,例如可以在HTML文件中搜索指定的元素,HTML中提取文本或元素屬性。
!!!!!注意  使用Requests-HTML首先要宣告Session

F12好用工具
這邊可以複製css選擇器或XPath選擇器



from requests_html import HTMLSession
#宣告Session
session=HTMLSession()

response=session.get('https://ithelp.ithome.com.tw/users/20134430/ironman/4307')
#確認response型別

print(type(response),type(response.html))

print(response.html.url)
#取得url, https://ithelp.ithome.com.tw/users/20134430/ironman/4307

print(response.html.links)
#取得reponse的所有連結,用集合回傳
print(type(response.html.links))

print(response.text)
#取得response的文字內容

print(response.html.text)
#取得reponse的文字內容,去除HTML的內容只留string部分

"""
Request-HTML 中可以使用 find() xpath() search() search_all() 進行資料的定位,下面進行分開介紹。
"""
element=response.html.find('.ir-profile-content')
#使用 css 選擇器定位資料

print(f'element={element}')#回傳一個list

print(f'這個列表中的內文:element[0].text={element[0].text}')

print('-'*30)
#使用 XPath 定位自料

element=response.html.xpath('/html/body/div[2]/div/div/div[2]/div[1]')
#注意!!! XPath的index都是從1開始

print(element) #看找到哪個標籤了
print(element[0].text) #解析裡面的字串
print('-'*30)

#使用search() 找尋符合的資料(只找第一個)
print(element[0].search('【Day {}】'), element[0].search('【Day {}】').fixed)
print('-'*30)

#使用 search_all() 找尋符合的資料(找尋全部)
print(element[0].search_all('【Day {}】'), element[0].search('【Day {}】').fixed)

#他會是一個list所以用迴圈取出它的結果
for day in element[0].search_all('【Day {}】'):
    print(day.fixed)
-------------------------------------------------- <class 'requests_html.HTMLResponse'> <class 'requests_html.HTML'> https://ithelp.ithome.com.tw/users/20134430/ironman/4307
{'https://ithelp.ithome.com.tw/2021ironman/ai-and-data', 'https://ithelp.ithome.com.tw/articles/10265829', 'https://ithelp.ithome.com.tw/users/20134430/traces/tags', 'https://ithelp.ithome.com.tw/users/20134430/responses', 'https://ithelp.ithome.com.tw/users/20134430/collections/ironman', 'https://ithelp.ithome.com.tw/articles/10268012', 'https://ithelp.ithome.com.tw/users/20134430/ironman/4307?page=2', 'https://ithelp.ithome.com.tw/users/login', 'https://ithelp.ithome.com.tw/users/20134430/traces/articles', 'https://ithelp.ithome.com.tw/guide', 'https://ithelp.ithome.com.tw/users/20134430/likes/answers', 'https://ithelp.ithome.com.tw/users/20134430/collections/questions', 'https://ithelp.ithome.com.tw/users/20134430/ironman', 'https://ithelp.ithome.com.tw/users/20134430/answers', 'https://ithelp.ithome.com.tw/articles/10271319', 'https://www.ithome.com.tw/', 'https://ithelp.ithome.com.tw/users/20134430/best_answers', 'https://ithelp.ithome.com.tw/talks', 'https://ithelp.ithome.com.tw/rss/series/4307', 'https://ithelp.ithome.com.tw/articles/10263628', 'https://ithelp.ithome.com.tw/articles/10269928', 'https://ithelp.ithome.com.tw/users/20134430/traces/users', 'https://ithelp.ithome.com.tw/users/20134430/questions', 'https://ithelp.ithome.com.tw/users/20134430/ironman/4307?page=3', 'https://ithelp.ithome.com.tw/tags', 'https://ithelp.ithome.com.tw/users/20134430/articles', 'https://ithelp.ithome.com.tw/users/20134430/likes/questions', 'https://ithelp.ithome.com.tw/users/20134430/collections/articles', 'https://ithelp.ithome.com.tw/questions', 'https://ithelp.ithome.com.tw/users/20134430/likes/responses', 'https://ithelp.ithome.com.tw/articles/10267300', 'https://ithelp.ithome.com.tw/messages/getGroup/20134430', '/', 'https://ithelp.ithome.com.tw/users/20134430/comments/questions', 'https://ithelp.ithome.com.tw/articles/10271887', 'https://ithelp.ithome.com.tw/users/20134430/talks', 'https://ithelp.ithome.com.tw/users/20134430/likes/articles', 'https://ithelp.ithome.com.tw/users/20134430/traced', 'https://ithelp.ithome.com.tw/articles?tab=tech', 'https://ithelp.ithome.com.tw/users/20134430/profile', 'https://ithelp.ithome.com.tw/articles/10268765', 'https://ithelp.ithome.com.tw/articles/10265084', 'https://ithelp.ithome.com.tw/users/20134430/invited', 'https://ithelp.ithome.com.tw/2021ironman/signup/team/169', '/2022ironman?utm_source=ithelp&utm_medium=navbar&utm_campaign=ironman14', 'https://ithelp.ithome.com.tw/users/20134430/traces/questions', 'https://www.ithome.com.tw/aboutus/', 'https://ithelp.ithome.com.tw/users/20134430/points', 'https://www.ithome.com.tw/terms', 'https://ithelp.ithome.com.tw/users/20134430/comments/responses', 'https://ithelp.ithome.com.tw/users/20134430/comments/answers', 'https://ithelp.ithome.com.tw/articles?tab=job', 'https://ithelp.ithome.com.tw/articles/10270687'}
<class 'set'>
..
..
        </div>

        <div class="ir-profile-series  ir-profile-series--done ">

                                            <a href="https://ithelp.ithome.com.tw/2021ironman/ai-and-data" class="group__badge group__badge--ai-and-data ir-profile-list__badge">
                    AI &amp; Data
                </a>
                        <h3 class="qa-list__title qa-list__title--ironman"> 
                網路爬蟲,萬物皆爬 - 30 天搞懂並實戰網路爬蟲及應對反爬蟲技術
<span> 系列</span>
            </h3>
...
..
網路爬蟲,萬物皆爬 - 30 天搞懂並實戰網路爬蟲及應對反爬蟲技術 :: 2021 iThome 鐵人賽 技術問答 技術文章 iT 徵才 Tag 聊天室 2022 鐵人賽 登入/註冊 問答 文章 Tag 邦友 鐵人賽 搜尋 Vincent55 (Vincent55) iT邦新手 4 級 ‧ 點數 439 無 9186 累計瀏覽數 24人 在追蹤 站內簡訊 追蹤 個人背景 0發問 .
.
..略
-------------------------------------------------- element=[<Element 'div' class=('ir-profile-content',)>] 這個列表中的內文:element[0].text=2021 iThome 鐵人賽 回列表 AI & Data 網路爬蟲,萬物皆爬 - 30 天搞懂並實戰網路爬蟲及應對反爬蟲技術 系列 你身為學生常在網路上或同學說網路爬蟲現在很流行,想知道這到底是甚麼?學習了 requests + beautifulsoup 爬蟲,想了解更多網路爬蟲技巧但缺乏實戰?或是辛辛苦苦 寫了一隻網路爬蟲,但被網頁的反爬蟲技術擋下? 30 天簡述多種類型網路爬蟲與原理,並佐以基礎知識及實戰演練。常見反爬蟲技術及 可行應對方案。 展開 收起 鐵人鍊成 | 共 30 篇文章 | 41 人訂閱 訂閱系列文 RSS系列文 |團隊肝已經,死 了 1 Like 1 留言 3335 瀏覽 DAY 1 達標好文 【Day 01】- 前言: 從 0 開始的網路爬蟲 前言 近年
.
.
...略
-------------------------------------------------- [<Element 'div' class=('ir-profile-content',)>] 2021 iThome 鐵人賽 回列表 AI & Data 網路爬蟲,萬物皆爬 - 30 天搞懂並實戰網路爬蟲及應對反爬蟲技術 系列 你身為學生常在網路上或同學說網路爬蟲現在很流行,想知道這到底是甚麼?學習了 requests + beautifulsoup 爬蟲,想了解更多網路爬蟲技巧但缺乏實戰?或是辛辛苦苦 寫了一隻網路爬蟲,但被網頁的反爬蟲技術擋下? 30 天簡述多種類型網路爬蟲與原理,並佐以基礎知識及實戰演練。常見反爬蟲技術及 可行應對方案。 展開 收起
.
.
..略
------------------------------ <Result ('01',) {}> ('01',) ------------------------------ [<Result ('01',) {}>, <Result ('02',) {}>, <Result ('03',) {}>, <Result ('04',) {}>, <Result ('05',) {}>, <Result ('06',) {}>, <Result ('07',) {}>, <Result ('08',) {}>, <Result ('09',) {}>, <Result ('10',) {}>] ('01',) ('01',) ('02',) ('03',) ('04',) ('05',) ('06',) ('07',) ('08',) ('09',) ('10',)

標籤:

0 個意見:

張貼留言

訂閱 張貼留言 [Atom]

<< 首頁