【发布时间】:2022-01-06 11:12:17
【问题描述】:
我正在尝试创建一个应用程序,该应用程序允许人们获取与他们提供的搜索关键字相关的 GitHub 存储库列表。在搜索查询的结果页面上,存储库有一个特殊的 div 类,即:
<div class="f4 text-normal">
</div>
如何让 Beautiful Soup 遍历页面上的所有这些类,然后遍历所有 <a> 标签以搜索 hrefs?
目前我只知道如何从<a>s 获取所有hrefs:
import requests, sys, webbrowser, bs4
#variables
linkList = []
#handle input
print('Your GitHub repository search query:')
userInput = input()
#get the results from the URL
results = requests.get('https://github.com/search?q=' + userInput + '&type=repositories'
+ ' '.join(sys.argv[1:]))
results.raise_for_status()
soup = bs4.BeautifulSoup(results.text, 'html.parser')
#find all the viable URLs
data = soup.find_all('a')
for aHref in data:
if "href" in str(aHref):
linkList.append(aHref)
print(linkList)
【问题讨论】:
标签: python beautifulsoup