有没有什么办法可以为beautifulsoup的select
方法提供多个参数?通过beautifulsoup css选择器获取多个标签
我正在通过soup.select('div[class^="TypeA"]'
检索数据。这让我把所有的div都匹配到模式TypeA
。我有兴趣检索,另外,另一个div class="TypeB"
(完全匹配)。
现在我可以在两个单独的通行证中做到这一点,例如,像这样:
r = requests.get(jurl)
soup = BeautifulSoup(r.text,"lxml")
list1 = []
#get typeA divs
for div in soup.select('div[class^="TypeA"]'):
t = [text for text in div.stripped_strings]
list1.append(t)
list2 = []
#get typeB divs
for div in soup.select('div[class^="TypeB"]'):
t = [text for text in div.stripped_strings]
list2.append(t)
#combine the two into tuples. Both lists are of the same size
list3 = []
count = 0
for item in list1:
list3.append((item,list2[count]))
count += 1
print list3
但是有可能做到一次?通过documentation,如何做到这一点并不明显。
你可以用'zip'功能'为DIV1,拉链DIV2(soup.select('DIV [^类=“TypeA”]'),soup.select('div [class^=“TypeB”]')):' – Arman