1. <source id="nejs2"></source>
  2. <video id="nejs2"></video>
    <source id="nejs2"></source>
          1. 首頁 > 筆記大全 > 用Python爬取JS

            用Python爬取JS

            更新:

            本文將從多個方面詳細闡述如何使用Python爬取JS,包括如何利用selenium庫模擬瀏覽器操作、如何使用puppeteer庫模擬Chrome瀏覽器、如何使用PyV8庫執行JS代碼、以及如何使用requests-html庫解析帶JS的網頁。希望本文能幫助讀者更好地了解如何應對JS代碼。

            一、selenium庫模擬瀏覽器操作

            selenium庫是一個自動化測試工具,在爬蟲中可以利用它模擬瀏覽器進行操作。使用selenium可以模擬真實的用戶操作,實現點擊、滾動、輸入等各種交互操作。

            以下是使用selenium爬取京東商品評論的例子:

            from selenium import webdriver
            
            options = webdriver.ChromeOptions()
            options.add_argument('--headless')  # 無頭模式,不彈出瀏覽器界面
            driver = webdriver.Chrome(chrome_options=options)
            driver.get('https://item.jd.com/100008348542.html#comment')
            
            for page in range(1, 11):
                js = f'javascript:commentVersionList.showPage({page});'
                driver.execute_script(js)
                comments = driver.find_elements_by_css_selector('.p-comment p')
                for comment in comments:
                    print(comment.text)
            

            在以上代碼中,使用selenium模擬打開京東商品評論的鏈接,然后利用execute_script()方法執行JS代碼實現翻頁功能,最后使用find_elements_by_css_selector()方法獲取評論內容。

            二、puppeteer庫模擬Chrome瀏覽器

            puppeteer是一個Node.js庫,但是可以使用pyppeteer庫在Python中使用。它可以模擬Chrome瀏覽器,支持JS執行、網絡請求、頁面截屏等操作。

            以下是使用pyppeteer爬取知乎熱榜的例子:

            import asyncio
            from pyppeteer import launch
            
            async def main():
                browser = await launch()
                page = await browser.newPage()
                await page.goto('https://www.zhihu.com/hot')
                await page.screenshot({'path': 'zhihu.png'})
                hot_list = await page.querySelectorAll('.HotItem-title')
                for item in hot_list:
                    print(await (await item.getProperty('textContent')).jsonValue())
                await browser.close()
            
            asyncio.get_event_loop().run_until_complete(main())
            

            在以上代碼中,使用pyppeteer模擬打開知乎熱榜的鏈接,截取頁面截圖,并使用querySelectorAll()方法獲取熱榜標題內容。

            三、PyV8庫執行JS代碼

            PyV8是一個Python封裝的V8 JavaScript引擎,可以執行JavaScript代碼,支持ECMAScript 5.1標準??梢杂脕砼廊в屑用躂S代碼的網頁。

            以下是使用PyV8解密boss直聘反爬蟲的例子:

            from PyV8 import JSContext, JSException
            
            ctx = JSContext()
            ctx.enter()
            
            js = "function decrypt(str){return unescape(str.replace(/&#x/g,'%u').replace(/;/g,''))}"
            decrypt_func = ctx.eval(js)
            
            crypt_str = 'V1jV%2F87%2BcnJHr5ABOAGzLZGIWp0kygfZzriBatmSCdReIx9YuNPo4XUQ3KFbqEw6sMTDv'
            decrypt_str = decrypt_func(crypt_str)
            
            print(decrypt_str)
            
            ctx.leave()
            

            在以上代碼中,使用PyV8執行JS代碼,實現對boss直聘反爬蟲的加密字符串進行解密。

            四、requests-html庫解析帶JS的網頁

            requests-html是Python的一個庫,它使用requests庫來執行HTTP請求,并使用pyppeteer庫來處理帶JS的網頁。requests-html始終在后臺運行一個Headless Chromium瀏覽器,并通過調用pyppeteer的API執行JS代碼,并將結果返回給requests-html。

            以下是使用requests-html爬取英雄聯盟Cosplay圖片的例子:

            from requests_html import HTMLSession
            
            url = 'https://bing.ioliu.cn/?p='
            session = HTMLSession()
            for page in range(1, 3):
                r = session.get(url + str(page))
                images = r.html.find('.card-img-top')
                for i, img in enumerate(images):
                    src = img.attrs['src']
                    title = img.attrs['title']
                    print('第%s頁 第%s張:%s' % (page, i+1, title))
                    print(src)
            

            在以上代碼中,使用requests-html模擬打開bing壁紙的鏈接,并使用find()方法獲取英雄聯盟Cosplay的圖片。

            頂部 久久久久99精品成人片毛片_黃色A片三級三級三級无码_日本不卡高清视频v中文字幕_高清欧美视频一区二区
            1. <source id="nejs2"></source>
            2. <video id="nejs2"></video>
              <source id="nejs2"></source>