Python3 webscraping代码

时间:2018-02-15 17:34:58

标签: python web-scraping

我有一个问题:

我有一个代码,可以打开一个网址并打印出源代码中的链接:

但是如何设法自动保存所有链接,如果有关键字,则此链接应保存在变量中:exMPLE。

link = "https://www.spiegel.de"
sauce = urllib.request.urlopen(link).read()
soup = bs.BeautifulSoup(sauce, 'lxml')
for url in soup.find_all('a'):
print(url.get('href'))

1 个答案:

答案 0 :(得分:0)

您可以使用href将所有href=True存储在(长)列表中以查找所有链接属性:

link = "https://www.spiegel.de"
sauce = urllib.request.urlopen(link).read()
soup = bs.BeautifulSoup(sauce, 'lxml') 
a = soup.find_all('a', href=True)

如果要打印列表中的每个单独链接,则应使用for循环。

相关问题