簡體   English   中英

美麗的湯不返回 HTML

[英]Beautiful Soup not returning HTML

我使用以下腳本從 html 頁面收集所有標簽,但它沒有顯示 html 響應,而是我得到了其他東西

import urllib.request
from bs4 import BeautifulSoup
loginurl= 'https://172.56.66.77'
fhand = urllib.request.urlopen(loginurl).read()
soup = BeautifulSoup(fhand,'html.parser')
print(soup)

我嘗試從 html 頁面收集特定數據,但是當我使用美麗的湯時,它沒有得到 html 數據,而是得到以下響應

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="xslt.cgi"?>
<iconmenu>
<title>Geräteinformationen</title><prompt>Geräteinformationen anzhhas</prompt>
<menuitem/><iconindex>-1</iconindex><name>MAC-Adresse :  76238823354</name><url></url>
<menuitem/><iconindex>-1</iconindex><name>Host-Name : SEP76238823354</name><url></url>
</iconmenu>

我無法過濾數據,因為它沒有顯示 html 標簽。

請幫助我從響應中獲取第二個數據SEP76238823354

事實證明,您只需要從構造函數調用中刪除第二個參數'html.parser'

import urllib.request
from bs4 import BeautifulSoup
xml_doc = """<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="xslt.cgi"?>
<iconmenu>
<title>Geräteinformationen</title><prompt>Geräteinformationen anzhhas</prompt>
<menuitem/><iconindex>-1</iconindex><name>MAC-Adresse :  76238823354</name><url></url>
<menuitem/><iconindex>-1</iconindex><name>Host-Name : SEP76238823354</name><url></url>
</iconmenu>"""
soup = BeautifulSoup(xml_doc)
print(soup.find_all("name")[1])
# -> <name>Host-Name : SEP76238823354</name>

只需 select 在這種情況下您需要的元素,通過包含主機名,通過分隔符split()它並抓住最后一部分:

...
soup = BeautifulSoup(fhand, 'xml')
soup.select_one('name:-soup-contains("Host-Name")').text.split(': ')[-1]

Output:

SEP76238823354

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM