Has*_*aig 5 python beautifulsoup
有没有办法为 beautifulsoup 的select方法提供多个参数?
我正在通过soup.select('div[class^="TypeA"]'. 这为我获取了类与模式匹配的所有 div TypeA。此外,我有兴趣检索另一个 div where 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
Run Code Online (Sandbox Code Playgroud)
但是有可能一次完成吗?通过文档,如何做到这一点并不是很明显。
soup.select('div[class^="TypeA"], div[class^="TypeB"]')
Run Code Online (Sandbox Code Playgroud)
使用,使用多个选择
| 归档时间: |
|
| 查看次数: |
668 次 |
| 最近记录: |