html 網頁源碼解析:bs4中BeautifulSoup


from bs4 import BeautifulSoup

result=requests.request("get","http://www.baidu.com")
result.encoding="utf-8" 
print(result.text)         #獲取源碼
soup=BeautifulSoup(result.text,"html.parser")      #解析html對象,並賦值給soup


soup.title        #獲取網頁第一個標簽為“title”內容
soup.title.string)     #獲取第一個標簽“title”的純字符串內容
soup.prettify()      #獲取html網頁源碼
soup.input["name"]    #獲取網頁第一個標簽為“input”內name的屬性
soup.input.name      #獲取標簽為input的名字,其實就是“input”
soup.input.attrs       #取網頁第一個標簽為“input”內所有屬性
soup.input["name"]="test"      #修改標簽內的屬性
del soup.input["name"]         #刪除標簽內的屬性
soup.input["name2"]="wq123"     #新增標簽內的屬性
soup.head.contents      #獲取標簽為head的內容
list(soup.head.children)      #獲取標簽為head的內容,與上面一致
list(soup.head.descendants)      #獲取標簽為head的內容(前面與上面一致,加上-1位號為title內容)
soup.head.parent       #獲取標簽為head父節點所有內容
soup.head.parent.parent     #獲取標簽為head父節點的父節點所有內容
soup.head.next_sibling       #獲取標簽head同級下一個兄弟節點
soup.head.previous_sibling      #獲取標簽head同級上一個兄弟節點
list(soup.head.next_siblings)      #獲取標簽head同級下一個兄弟節點存儲為生成器
list(soup.head.previous_siblings)      #獲取標簽head同級上一個兄弟節點存儲為生成器
soup.find_all("a",class_="js_a_so")        #獲取標簽為a,class屬性為"js_a_so"的所有標簽對象  

 


免責聲明!

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



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