我已经解析了网页中的所有 "href" 个链接。我只想用我的浏览器打开其中一个。我怎样才能做到这一点?
I have parsed all "href" links from a webpage. I want to open only one of them with my browser. How can I do that?
我想解析其中包含单词 'cricket' 的 link。
这是代码。
import urllib.request
import re
from bs4 import BeautifulSoup
import webbrowser
url = "http://www.thehindu.com/"
def hi():
dep = urllib.request.urlopen(url)
soup = BeautifulSoup(dep, "html.parser")
#to parse links that contain word bangladesh in it
for link in soup.find_all('a', href=re.compile("bangladesh")):
tip = link.get('href')
print(tip)
webbrowser.open(tip)
hi()
[这是输出。我想用我的网络浏览器打开输出的第二个 link(并忽略第一个)][1]
如果你想打开第二个link,你可以这样做:
for pos, link in enumerate(soup.find_all('a', href=re.compile("bangladesh"))):
tip = link.get('href')
print(tip)
if (pos + 1) == 2:
webbrowser.open(tip)
我想解析其中包含单词 'cricket' 的 link。 这是代码。
import urllib.request
import re
from bs4 import BeautifulSoup
import webbrowser
url = "http://www.thehindu.com/"
def hi():
dep = urllib.request.urlopen(url)
soup = BeautifulSoup(dep, "html.parser")
#to parse links that contain word bangladesh in it
for link in soup.find_all('a', href=re.compile("bangladesh")):
tip = link.get('href')
print(tip)
webbrowser.open(tip)
hi()
[这是输出。我想用我的网络浏览器打开输出的第二个 link(并忽略第一个)][1]
如果你想打开第二个link,你可以这样做:
for pos, link in enumerate(soup.find_all('a', href=re.compile("bangladesh"))):
tip = link.get('href')
print(tip)
if (pos + 1) == 2:
webbrowser.open(tip)