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 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和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或正则表达式更合适,我也会对这些解决方案持开放态度.
我想知道我是否可以使用漂亮的汤,将 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来排列几条线吗?
我想通过html文本中的美味汤找到一个特定单词在网页中出现了多少次?我尝试了这个findAll函数,但只发现特定标签中soup.body.findAll的单词会在body标签中找到特定的单词,但我希望它在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)
为什么不找到"名字"?
谢谢!
python ×6
python-2.7 ×2
python-3.x ×2
regex ×2
html ×1
html-parsing ×1
lxml ×1
string ×1
urllib2 ×1