相关疑难解决方法(0)

在Python中查找所有出现的子字符串

Python已经string.find()并且string.rfind()在字符串中获取子字符串的索引.

我想知道,也许有类似的东西string.find_all()可以返回所有已创建的索引(不仅从开始或从头到尾)?

例如:

string = "test test test test"

print string.find('test') # 0
print string.rfind('test') # 15

#this is the goal
print string.find_all('test') # [0,5,10,15]
Run Code Online (Sandbox Code Playgroud)

python regex string

325
推荐指数
12
解决办法
39万
查看次数

需要为Python 3.5.1安装urllib2

我正在运行Python 3.5.1 for Mac.我想用urllib2.我尝试安装它,但我被告知它已被拆分为urllib.request和urllib.error for Python 3.

我的命令(暂时从框架bin目录运行,因为它不在我的路径中):

sudo ./pip3 install urllib.request
Run Code Online (Sandbox Code Playgroud)

返回:

Could not find a version that satisfies the requirement urllib.request (from versions: )
No matching distribution found for urllib.request
Run Code Online (Sandbox Code Playgroud)

我在尝试一举安装urllib2之前遇到了同样的错误.

python urllib2 python-3.x

82
推荐指数
3
解决办法
30万
查看次数

在HTML BeautifulSoup中按文本查找并替换

我正在尝试使用python和BeautifulSoup标记一个HTML文件(字面上用"mark"标签包装字符串).问题基本如下......

说我有我原来的html文档:

test = "<h1>oh hey</h1><div>here is some <b>SILLY</b> text</div>"
Run Code Online (Sandbox Code Playgroud)

我想对本文档中的字符串进行不区分大小写的搜索(忽略HTML)并将其包装在"mark"标记中.所以我想说我想在html中找到"这里有一些愚蠢的文字"(忽略粗体标签).我想把匹配的html包装在"mark"标签中.

例如,如果我想在测试中搜索"这里有一些愚蠢的文本" ,那么所需的输出是:

"<h1>oh hey</h1><div><mark>here is some <b>SILLY</b> text</mark></div>"
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?如果使用lxml或正则表达式更合适,我也会对这些解决方案持开放态度.

python regex lxml beautifulsoup html-parsing

7
推荐指数
1
解决办法
2136
查看次数

如何使用 BeautifulSoup 查找元素的父标签?

我想知道我是否可以使用漂亮的汤,将 html 行排成几行:

<tr id="12590559" class="">
<td>
<span class="he16-1 tip-top" title="Cracker"></span>
</td>
<td>
cracker.crc
</td>
Run Code Online (Sandbox Code Playgroud)

在那个例子中,我想提取 id 但使用标题信息:

soup = BeautifulSoup(lista.content, "lxml")
id = soup.find(attrs={"title": "Cracker"})
Run Code Online (Sandbox Code Playgroud)

我可以得到

soup = BeautifulSoup(lista.content, "lxml")
id = soup.find(attrs={"title": "Cracker"})
Run Code Online (Sandbox Code Playgroud)

但我也想得到id. 我可以用BeautifulSoup来排列几条线吗?

python beautifulsoup python-3.x

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

如何通过python中的美丽汤找到html页面中的特定单词?

我想通过html文本中的美味汤找到一个特定单词在网页中出现了多少次?我尝试了这个findAll函数,但只发现特定标签中soup.body.findAll的单词会在body标签中找到特定的单词,但我希望它在html文本中的所有标签中搜索该单词.另外,一旦我找到了这个词,我需要在该词之前和之后创建一个单词列表,有人可以帮我怎么做?谢谢.

python beautifulsoup python-2.7

2
推荐指数
1
解决办法
7358
查看次数

在HTML中查找和替换字符串

从这个HTML代码:

<p class="description" dir="ltr">Name is a fine man. <br></p>
Run Code Online (Sandbox Code Playgroud)

我正在寻找使用以下代码替换"名称":

target = soup.find_all(text="Name")
for v in target:
    v.replace_with('Id')
Run Code Online (Sandbox Code Playgroud)

我想要的输出是:

<p class="description" dir="ltr">Id is a fine man. <br></p>
Run Code Online (Sandbox Code Playgroud)

当我:

print target
[]
Run Code Online (Sandbox Code Playgroud)

为什么不找到"名字"?

谢谢!

html python beautifulsoup python-2.7

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