【发布时间】:2013-02-11 10:20:53
【问题描述】:
我需要的数据存在于tag + class 的两种不同组合中。我希望我的函数在这两种组合下进行搜索并同时呈现这两种组合下的数据。这两种组合是互斥的。如果存在 1 个组合,则不存在其他组合。
我使用的代码是:
# -*- coding: cp1252 -*-
import csv
import urllib2
import sys
import urllib
import time
from bs4 import BeautifulSoup
from itertools import islice
def match_both2(arg1,arg2):
if arg1 == 'div' and arg2 == 'DetailInternetFirstContent empty openPostIt':
return True
if arg1 == 'p' and arg2 == 'connection':
return True
return False
page = urllib2.urlopen('http://www.sfr.fr/mobile/offres/toutes-les-offres-sfr?vue=000029#sfrintid=V_nav_mob_offre-abo&sfrclicid=V_nav_mob_offre-abo').read()
soup = BeautifulSoup(page)
datas = soup.findAll(match_both2(0),{'class':match_both2(1)})
print datas
现在,我正在尝试使用 match_both2 函数来完成此操作,但它给了我 TypeError 因为我只向它传递了 1 个参数并且它需要 2 个。我不知道在这种情况下如何将 2 个参数传递给它,通常我会调用类似 match_both2(example1,example2) 的函数。但是在这里,我想不出一种可以解决我的问题的方法。
请帮我解决这个问题。
【问题讨论】:
标签: python python-2.7 beautifulsoup