mylist = ["aa123", "bb2322", "aa354", "cc332", "ab334", "333aa"]
Run Code Online (Sandbox Code Playgroud)
我需要包含'aa'的所有项目的索引位置.我在将enumerate()与部分字符串匹配相结合时遇到了麻烦.我甚至不确定我是否应该使用枚举.
我只需要返回指数位置:0,2,5
由于beautifulsoup,我已经抓取了网络数据,但是我无法将输出转换为可以操作的矩阵/数组.
from bs4 import BeautifulSoup
import urllib2
headers = { 'User-Agent' : 'Mozilla/5.0' }
req = urllib2.Request('http://statsheet.com/mcb/teams/duke/game_stats', None, headers)
html = urllib2.urlopen(req).read()
soup = BeautifulSoup(html)
#statdiv = soup.find('div', attrs={'id': 'basic_stats'}) #not needed
table = soup.find('table', attrs={'class': 'sortable statsb'})
rows = table.findAll('tr')
for tr in rows:
text = []
cols = tr.findAll('td')
for td in cols:
try:
text = ''.join(td.find(text=True))
except Exception:
text = "000"
print text+",",
print
Run Code Online (Sandbox Code Playgroud)
注意:''.join(td.find(text=True))是为了防止程序在空白单元格上失败.
哪个输出:
W, GSU, 32, 42, 74, 24-47, 51.1, 15-23, 65.2, …Run Code Online (Sandbox Code Playgroud)