[英]Soup.select method in python - how to choose only one score?
I want to display only first score of soup.select method, now i have this code:我只想显示soup.select方法的第一个分数,现在我有这个代码:
score = soup.select('my_name a')
print(score)
urls = [tag['href'] for tag in score if 'href' in tag.attrs and "name" in tag['href']]
print(urls)
When bs finds only one score, it works very good.当 bs 只找到一个分数时,效果很好。 But how to choose only first score when i get more scores?但是当我获得更多分数时如何只选择第一个分数?
Now output is:现在输出是:
> [<a href="/name/adrian1021 "> Adrian </a>, <a href="/name/john.james"> John James </a>, <a href="/name/carol22 "> Carol Coat </a>]
>
> ['/name/adrian1021 ', '/name/john.james ', '/name/carol22 ']
I want to extract only one score, for example output should be:我只想提取一个分数,例如输出应该是:
[/name/adrian1021 ']
Is that possible?那可能吗? I tried to work on this like on list but it does not work.我试图像在列表中一样处理这个,但它不起作用。
You are looking to use soup.select_one()
which finds only the first element that matches a selector.您正在寻找使用soup.select_one()
只查找与选择器匹配的第一个元素。
So your score variable should be:所以你的分数变量应该是:
score = soup.select_one('my_name a')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.