[英]How do I get certain links from a website, but not all of them?
這是我到目前為止所擁有的:
import requests
from bs4 import BeautifulSoup
def linkScraper():
html = requests.get("https://www.bbc.com/").text
soup = BeautifulSoup(html, 'html.parser')
for link in soup.find_all('a'):
print(link.get('href'))
但這會打印網站上的每個鏈接。 我如何配置它以提供指向出現在 BBC 主頁上的文章的鏈接?
您可以使用列表理解對其進行過濾:
links = [link for link in soup.find_all('a') if link.startswith('https://www.bbc.com/')]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.