【发布时间】:2016-08-17 03:00:56
【问题描述】:
这里是新用户。我开始掌握 Python 语法的窍门,但一直被 for 循环所抛弃。我了解到目前为止我在 SO 上遇到的每个场景(以及我之前的示例),但似乎无法为我当前的场景想出一个。
我正在使用 BeautifulSoup 从应用商店中提取特征作为练习。
我创建了一个包含 GooglePlay 和 iTunes 网址的列表以供使用。
list = {"https://play.google.com/store/apps/details?id=com.tov.google.ben10Xenodromeplus&hl=en",
"https://play.google.com/store/apps/details?id=com.doraemon.doraemonRepairShopSeasons&hl=en",
"https://play.google.com/store/apps/details?id=com.KnowledgeAdventure.SchoolOfDragons&hl=en",
"https://play.google.com/store/apps/details?id=com.turner.stevenrpg&hl=en",
"https://play.google.com/store/apps/details?id=com.indigokids.mimdoctor&hl=en",
"https://play.google.com/store/apps/details?id=com.rovio.gold&hl=en",
"https://itunes.apple.com/us/app/angry-birds/id343200656?mt=8",
"https://itunes.apple.com/us/app/doodle-jump/id307727765?mt=8",
"https://itunes.apple.com/us/app/tiny-wings/id417817520?mt=8",
"https://itunes.apple.com/us/app/flick-home-run-!/id454086751?mt=8",
"https://itunes.apple.com/us/app/bike-race-pro/id510461370?mt=8"}
为了测试beautifulsoup(我的代码中的bs),我为每个商店使用了一个应用程序:
gptest = bs(urllib.urlopen("https://play.google.com/store/apps/details?id=com.rovio.gold&hl=en"))
ios = bs(urllib.urlopen("https://itunes.apple.com/us/app/doodle-jump/id307727765?mt=8"))
我在 iTunes 上找到了一个应用的类别:
print ios.find(itemprop="applicationCategory").get_text()
...在 Google Play 上:
print gptest.find(itemprop="genre").get_text()
有了这种新的信心,我想尝试遍历我的整个列表并输出这些值,但后来我意识到我很讨厌 for 循环......
这是我的尝试:
def opensite():
for item in list:
bs(urllib.urlopen())
for item in list:
try:
if "itunes.apple.com" in row:
print "Category:", opensite.find(itemprop="applicationCategory").get_text()
else if "play.google.com" in row:
print "Category", opensite.find(itemprop="genre").get_text()
except:
pass
注意:理想情况下,我会传递一个 csv(称为“样本”,其中有一列“URL”),所以我相信我的循环将从
for row in sample.URL:
但我认为向您展示列表比处理数据框更有帮助。
提前致谢!
【问题讨论】:
标签: python for-loop beautifulsoup urlopen