【发布时间】:2020-05-31 22:09:24
【问题描述】:
我正在尝试收集输入,我希望打印第一个链接匹配项。我似乎无法弄清楚,所以我在这里。任何想法都会非常感谢
import bs4
from urllib.request import urlopen
from bs4 import BeautifulSoup
import re
import requests
result = requests.get("https://naruto-arena.net/characters-and-skills")
src = result.content
soup = BeautifulSoup(src, 'lxml')
links = soup.find_all("a", { "class" : "subjlink"})
name = input("Enter name: ")
for link in links:
href = (link.attrs['href'])
if name.lower() in href[30:].replace("-", "").lower():
print(href)
结果:
输入姓名:火影忍者
https://naruto-arena.net/char/Uzumaki-Naruto
https://naruto-arena.net/char/Kyuubi-Naruto
https://naruto-arena.net/char/Uzumaki-Naruto-(S)
https://naruto-arena.net/char/Sennin-Naruto-(S)
https://naruto-arena.net/char/Four-Tail-Kyuubi-Naruto-(S)
https://naruto-arena.net/char/Kyuubi-Naruto-(S)
想要的结果:
【问题讨论】:
标签: python-3.x beautifulsoup request python-re