requests-html的安裝與使用


使用requests-html單一庫完成簡書首頁獲取(包含請求、解析、JavaScript渲染功能)

一:安裝

sudo pip install requests-html

# -*- coding: utf-8 -*-
import requests_html


 
text_url = 'https://music.line.me/album/mb00000000012ac6c9' 
user_agent = requests_html.user_agent()
print("User-Agent:",user_agent) 
session = requests_html.HTMLSession()
headers = {
    "User-Agent":user_agent
} 
r = session.get(text_url,headers=headers) 
r.html.render(sleep=1)
print(r.html.html)  

r.html.render() 在初次使用該功能的時候會自動下載支持包:Chromium,但是由於國內網網絡的原因,異常慢,基本失敗,需要改寫下載源,找到python3里的Pyppeteer的chromium_downloader.py里的下載路徑,在http://npm.taobao.org/mirrors/chromium-browser-snapshots/Linux_x64/上面找最新的路勁寫上,shell界面輸入pyppeteer-install進行安裝。

二、使用

更多使用以后在記錄,本次着重把它跑通即可。

requeests_html報錯pyppeteer.errors.BrowserError: Browser closed unexpectedly:解決辦法

這個一般是環境問題,ubuntu運行以下代碼試下

sudo apt-get install  gconf-service libasound2 libatk1.0-0 libatk-bridge2.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM