如何使用python-requests显示特定的HTML代码? 这是我的代码:
import requests
s = requests.Session()
print("Made by Zaseth.")
shodanURL = input("Your target IP: ")
s.get('https://www.shodan.io/host/' + shodanURL)
r = s.get('https://www.shodan.io/host/' + shodanURL)
print(r.text)
print(r)
print(r.headers)
print(r.status_code)
with open('output.txt','w') as fd:
fd.write(r.text)
现在这将在s.get上添加用户输入并将完整的HTML响应保存到output.txt中,这一切都很好,但我的回复是完整的HTML页面。我只想在页面上显示特定数据,如:
<meta name="twitter:description" content="Ports open: 53, 80, 110, 143, 443, 465, 993, 995, 2082, 2083, 2087, 3306, 8080, 8443"/>
这是Shodan.io显示所有开放端口的标记。 以下是我想要显示的更多数据:
<h2><i class="fa fa-globe"></i>the ip
<small style="padding-left:10px;">the host</small>
</h2><span class="badge badge-inverse">something special like Database</span>
这可能吗?
答案 0 :(得分:0)
如果要检索的行的位置处于静态,那么我认为最简单的方法是使用 split。
line = 18 # html line you want to fetch
print(r.text.splitlines()[line - 1])
# <meta name="twitter:description" content="Ports open: 80, 443, 2079, 2082, 2086, 2087" />
正则表达式的另一种方式:
print(re.findall('(<meta name="twitter:description(.*)>)', r.text))