【发布时间】:2021-02-16 16:02:38
【问题描述】:
我想在学校网站上每 7 年提取一次链接。在档案中,使用 ctrl + f "year-7" 很容易找到。不过,beautifulSoup 并不是那么容易。或者我做错了。
import requests
from bs4 import BeautifulSoup
URL = '~school URL~'
page = requests.get(URL)
soup = BeautifulSoup(page.content, 'html.parser')
for link in soup.find_all('a'):
print(link.get('href'))
这为我提供了网站档案中的每个链接。对我来说重要的每个链接都差不多是这样的:
~school URL~blogspot.com/2020/10/mathematics-activity-year-x.html
我尝试将“(link.get('href'))”存储在一个变量上并在其上搜索“year-x”,但这不起作用。
关于如何搜索它的任何想法? Blogspot 搜索是可怕的。我这样做是为了帮助贫困地区的孩子更轻松地找到他们的课程,因为它们都只是留在了下一学年的网站上,并且有数百个没有标签的链接用于不同的学年。我正在尝试至少为每个学年编制一份链接列表以帮助他们。
【问题讨论】:
-
所以如果我明白了,你想获得
year-7的所有链接? -
是的!然后在第 8 年、第 9 年进行...
-
你能用其他 HTML 链接编辑你的问题吗(你想要什么和不想要什么)?
-
它会是这样的:``` ~school URL~blogspot.com/2020/10/geography-activity-year-1.html ~school URL~blogspot.com/2020/10 /history-activity-year-3.html ~school URL~blogspot.com/2020/10/english-activity-year-8.html ``` 等等。这一切都混在一起了。
-
实际的 HTML 标记是什么?
标签: python beautifulsoup automation