使用漂亮的汤爬行html

2024-04-26 01:33:41 发布

您现在位置:Python中文网/ 问答频道 /正文

#searchMenu > div.menu_wrap > ul > li.first.actived > div > div > div > 
div.menu_con.ng-scope > ul > li.actived > div > div.result_list > div > ul 
> li:nth-child(1) > ul > li:nth-child(3)

所以我想提取所有这样的元素。你知道吗

li:nth-child(1) > ul > li:nth-child(3)
li:nth-child(2) > ul > li:nth-child(3)
li:nth-child(3) > ul > li:nth-child(3)
li:nth-child(4) > ul > li:nth-child(3)
li:nth-child(5) > ul > li:nth-child(3)

如何使用“for循环”生成代码?你知道吗

我刚试过这个代码:

address = [] 

for book in tags: 
    tag = book.select_one('li:nth-of-type(4) ').text address.append(tag)
    print(address)

Tags: 代码divchildforaddresstagliul
1条回答
网友
1楼 · 发布于 2024-04-26 01:33:41

枚举标记并将索引值传递给选择器。像这样

for i, book in enumerate(tags): 
    tag = book.select_one('li:nth-of-type('+str(i)+') ').text address.append(tag)
    print(address)

希望这有帮助!干杯!你知道吗

相关问题 更多 >