Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Indexing urllib2:索引多个关键字_Indexing_Python 2.7_Urllib2 - Fatal编程技术网

Indexing urllib2:索引多个关键字

Indexing urllib2:索引多个关键字,indexing,python-2.7,urllib2,Indexing,Python 2.7,Urllib2,我已经四处寻找了一段时间,似乎找不到使用urllib2中的index命令的指南。 基本上,该函数查找第一个单词,该单词是/或包含索引函数给定的字符,然后返回该单词的位置 page_content= urllib2.urlopen(URL).read() #Gets the website i1 = page_content.index(keyword) #finds keyword in the website 从这里开始,i1可以用于urllib2中的不同内容,但这与此无关 我的问题是,索引

我已经四处寻找了一段时间,似乎找不到使用urllib2中的index命令的指南。 基本上,该函数查找第一个单词,该单词是/或包含索引函数给定的字符,然后返回该单词的位置

page_content= urllib2.urlopen(URL).read() #Gets the website
i1 = page_content.index(keyword) #finds keyword in the website
从这里开始,i1可以用于urllib2中的不同内容,但这与此无关

我的问题是,索引并没有得到所有与关键字匹配的单词,而是只得到第一个。如果你知道网站上只有一个这样的网站,这是没有问题的,但是我想把他们都分配到一个列表中,然后我可以从中获取他们的位置并使用。

用户重新运行

import re 
page_content= urllib2.urlopen(URL).read() #Gets the website
idall = [m.start() for m in re.finditer(keyword,page_content)]

print idall

当我修改代码以从idall中获取所有值时,工作就完美无缺了。非常感谢。