如何获得网站上所有文章的链接?

时间:2020-01-21 00:41:52

标签: python selenium web-scraping beautifulsoup

嗨,我真的是BS4或硒的新手。我想知道是否有一种方法可以获取网站上所有文章的链接。

例如https://uk.yahoo.com将有许多新闻文章。我如何(或有可能)获得所有这些文章的链接列表?

1 个答案:

答案 0 :(得分:1)

尝试一下。添加您自己的用户代理字符串。

import re
import requests
from bs4 import BeautifulSoup

response = requests.get(url='https://uk.yahoo.com ', headers={'User-Agent':''})
soup = BeatifulSoup(response.content, 'html.parse')

links = []
for link in soup.findAll('a', attrs={'href': re.compile('^https://')}
    links.append(link.get('href'))
print(links)
相关问题