这里是我迄今为止的Python美丽的汤 - 功能
import urllib2
from ntlm import HTTPNtlmAuthHandler
from bs4 import BeautifulSoup
import requests
import os
import bleach
def stripAllTags(html):
if html is None:
return None
return ''.join(BeautifulSoup(html).findAll(text = True))
os.system('clear')
user = '<user>'
password = "<pass>"
url = "<some url>"
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, url, user, password)
# create the NTLM authentication handler
auth_NTLM = HTTPNtlmAuthHandler.HTTPNtlmAuthHandler(passman)
# create and install the opener
opener = urllib2.build_opener(auth_NTLM)
urllib2.install_opener(opener)
data = urllib2.urlopen(url)
soup = BeautifulSoup(data)
table = soup.find('ul', {'class': 'dfwp-column dfwp-list'})
td = table.findAll('td')
tr = table.findAll('tr')
for td in table:
for tr in td:
clean = bleach.clean(tr, tags=[], strip=True)
print clean
我怎样才能正确地把它变成一个功能
table = soup.find('ul', {'class': 'dfwp-column dfwp-list'})
td = table.findAll('td')
tr = table.findAll('tr')
for td in table:
for tr in td:
clean = bleach.clean(tr, tags=[], strip=True)
print clean
我想它调用一个“为”
你究竟想要'for'做什么?这部分代码应该只需要一次在任何脚本 – Amazingred
我仍在构建这个,但此脚本解析来自我们客户的服务器信息共享点页面,所以我将需要为每个发现的客户做到这一点 – ondrovic
您的功能将要做许多不必要的操作。你有for循环的问题。查看我的更新回答下面的完整runthrough – Amazingred