我如何使用BeautifulSoup搜索仅包含我搜索的属性的标签?
例如,我想找到所有<td valign="top">
标签.
以下代码:
raw_card_data = soup.fetch('td', {'valign':re.compile('top')})
获取我想要的所有数据,但也获取<td>
具有该属性的任何标记valign:top
我也试过了:
raw_card_data = soup.findAll(re.compile('<td valign="top">'))
这没有任何回报(可能是因为正则表达式不好)
我想知道在BeautifulSoup中是否有一种方法可以说"查找<td>
唯一属性为valign:top
"的标签
更新
例如,如果HTML文档包含以下<td>
标记:
<td valign="top">.....</td><br />
<td width="580" valign="top">.......</td><br />
<td>.....</td><br />
Run Code Online (Sandbox Code Playgroud)
我只想要第一个<td>
tag(<td width="580" valign="top">
)返回
我在这里浏览了python日志类的教程,并没有看到任何可以让我为同一输出制作不同级别的多个日志的东西.最后我想有三个日志:(
<timestamp>_DEBUG.log
调试级别)
<timestamp>_INFO.log
(信息级别)
<timestamp>_ERROR.log
(错误级别)
有没有办法在一个脚本中为同一输入生成多个日志文件?
<-------------更新#1 -------------------------->
所以在实施@ robert的建议时,我现在有一个小问题,可能是由于没有完全理解他的代码中正在做什么.
这是我在scriptRun.py中的代码
import os
import logging
logger = logging.getLogger("exceptionsLogger")
debugLogFileHandler = logging.FileHandler("Debug.log")
errorLogFileHandler = logging.FileHandler("Error.Log")
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
errorLogFileHandler.setFormatter(formatter)
debugLogFileHandler.setFormatter(formatter)
logger.addHandler(debugLogFileHandler)
logger.addHandler(errorLogFileHandler)
class LevelFilter(logging.Filter):
def __init__(self, level):
self.level = level
def filter(self, record):
return record.levelno == self.level
debugLogFileHandler.addFilter(LevelFilter(logging.DEBUG))
errorLogFileHandler.addFilter(LevelFilter(logging.ERROR))
directory = []
for dirpath, dirnames, filenames in os.walk("path\to\scripts"):
for filename in [f for f in filenames if f.endswith(".py")]:
directory.append(os.path.join(dirpath, filename))
for entry in directory:
execfile(entry)
for lists …
Run Code Online (Sandbox Code Playgroud) 所以我有一个bash脚本需要采用任意数量的命令行参数并将它们放入一个字符串中
用户输入内容的示例:
give <environment> <email> <any number of integers separated by spaces>
give testing stuff@things.com 1 2 3 4 5
Run Code Online (Sandbox Code Playgroud)
我希望得到从$ 3到$#的所有参数,并将它们连接成一个字符串.
我现在的(可能很糟糕的)解决方案是
if [ $# -gt 3 ]
then
env="env="$1
email="email="$2
entList=""
for i in {3..$#}
do
if [ $i -eq 3 ]
then
entList=$3
shift
fi;
if [ $i -gt 3 ]
then
entList=$entList","$3
shift
fi;
done
fi;
Run Code Online (Sandbox Code Playgroud)
我处理只有三个参数有点不同的情况,并且一个工作正常.
$entList
给出这个例子的最终值give testing stuff@things.com 1 2 3 4 5
应该是:1,2,3,4,5
现在,当我运行这个时,我得到以下错误:
/usr/local/bin/ngive.sh: line 29: [: {3..5}: integer …
Run Code Online (Sandbox Code Playgroud) 我有以下脚本将在目录中运行每个脚本(顺序):
import os
directory = []
for dirpath, dirnames, filenames in os.walk("path\to\scripts"):
for filename in [f for f in filenames if f.endswith(".py")]:
directory.append(os.path.join(dirpath, filename))
for entry in directory:
execfile(entry)
print x
Run Code Online (Sandbox Code Playgroud)
我的脚本看起来像这样:
def script1():
x = "script 1 ran"
return x
script1()
Run Code Online (Sandbox Code Playgroud)
当print x
被调用时,它说,X没有定义.我只是好奇是否有办法返回值,以便父脚本可以访问数据.
我有我的表:把它tblA
这个表有三行,id
,sub-id
,和visibility
sub-id
是主键(它定义了分类法id
).我正在尝试构建一个查询,选择每次id
出现少于三次的查询.
这是一个示例查询/结果
select * from tbla where id = 188002;
+--------+--------+-------------+
| sub-id | id | visibility |
+--------+--------+-------------+
| 284922 | 188002 | 2 |
| 284923 | 188002 | 2 |
| 284924 | 188002 | 0 |
+--------+--------+-------------+
Run Code Online (Sandbox Code Playgroud)
从我在这里和这里看到的看起来我需要加入表格......本身.我真的不明白这完成了什么.
如果有人对此有所了解,我们将不胜感激.我将继续研究它并使用我遇到的任何其他信息更新此主题.
谢谢