Beautiful Soup使用,登入知乎

2021-08-10 13:15:31 字數 1337 閱讀 4001

beautiful soup提供一些簡單的、python式的函式用來處理導航、搜尋、修改分析樹等功能。它是乙個工具箱,通過解析文件為使用者提供需要抓取的資料,因為簡單,所以不需要多少**就可以寫出乙個完整的應用程式。

beautiful soup自動將輸入文件轉換為unicode編碼,輸出文件轉換為utf-8編碼。你不需要考慮編碼方式,除非文件沒有指定乙個編碼方式,這時,beautiful soup就不能自動識別編碼方式了。然後,你僅僅需要說明一下原始編碼方式就可以了。

beautiful soup已成為和lxml、html6lib一樣出色的python直譯器,為使用者靈活地提供不同的解析策略或強勁的速度

from bs4 import beautifulsoup

from urllib import request

import requests

import time

#defcaptcha

(captcha_data):

#將2進製資料寫入到檔案中

text=input('請輸入驗證碼')

return text

defzhihulogin

():#構建乙個session可以儲存cookie

sess=requests.session()

headers=

#首先獲取登入頁面,找到需要post的資料,同時記錄cookie的值

html=sess.get('',headers=headers).text

#呼叫xml解析庫

bs=beautifulsoup(html,'lxml')

#_xsrf作用是跨站請求偽造(或者叫跨域攻擊)

_xsrf=bs.find('input',attrs=).get('value')

print(_xsrf)

url=''

captcha_url=''%(time.time()*1000)

print(captcha_url)

#傳送圖請求,獲取資料流。

captchadata=sess.get(captcha_url,headers=headers).content

text=captcha(captchadata)

data=

response=sess.post('',data=data,headers=headers)

print(response.text)

if __name__=='__main__':

zhihulogin()

BeautifulSoup 安裝使用

linux環境 1.安裝 方法一 解壓 tar xzvf beautifulsoup4 4.2.0.tar.gz 安裝 進入解壓後的目錄 python setup.py build sudo python setup.py install 方法二 快速安裝 ubuntu sudo apt get i...

BeautifulSoup使用相關知識

1基礎使用,獲取某一 內容的h1標籤 2複雜html解析 print name.get text get text 清除標籤,只保留內容 4通過網際網路採集 外鏈 from urllib.request import urlopen from bs4 import beautifulsoup imp...

使用BeautifulSoup解析HTML

通過css屬性來獲取對應的標籤,如下面兩個標籤 可以通過class屬性抓取網頁上所有的紅色文字,具體 如下 from urllib.request import urlopen from bs4 import beautifulsoup html urlopen bsobj beautifulsou...