小编Ran*_*jan的帖子

使用带有beautifulsoup的python 2.7在html页面中标记的位置

我试图用给定的格式解析一个html页面:

<img class="outer" id="first" />
<div class="content" .../>
<div class="content" .../>
<div class="content" />
<img class="outer" id="second" />
<div class="content" .../>
<div class="content" .../>
<img class="outer" id="third" />
<div class="content" .../>
<div class="content" .../>
Run Code Online (Sandbox Code Playgroud)

当迭代div标签时,我想弄清楚当前的div标签是否在id为'first','second'或'third'的img标签下.有没有办法做到这一点?我有img块和div块的列表:

img_blocks = soup.find_all('img', attrs={'class':'outer'})
div_Blocks = soup.find_all('div', attrs={'class':'content'})
Run Code Online (Sandbox Code Playgroud)

python beautifulsoup python-2.7

5
推荐指数
1
解决办法
870
查看次数

从sys.argv中删除已解析的选项及其值

我试图使用optparse(解析我的脚本的命令行选项)和fileinput(以便通过管道或文件提供数据输入的灵活性).

import optparse, fileinput

parser = OptionParser()    
parser.add_option("-v", action="store_true", dest="verbose")
(options, args) = parser.parse_args()

for line in fileinput.input:
     process(line)
Run Code Online (Sandbox Code Playgroud)

但是fileinput尝试使用'-v'选项以及文件名导致'没有这样的文件或目录错误'.所以要么我需要创建fileinput args,要么从sys.argv中删除已解析的选项,但是我不知道这样做的优雅方式.有什么指针吗?

python file-io optparse python-2.5

4
推荐指数
1
解决办法
2142
查看次数