【问题标题】:Python deal with resultsetPython处理结果集
【发布时间】:2023-04-07 02:34:02
【问题描述】:

这里是Python初学者,我想按顺序打印n个链接,n是未知的,本例我打印[0]为一个视图,如何制作?

soup=BeautifulSoup(text,'html.parser')
for link in soup.select('.list-group'):
    items=link.select('a')#.to_list()
    print(endpoint+"/"+link.select('a')[0]['href'])
    #print(link.select('a'))
    print(len(items))
    print(type(items))
    print(items)

输出如下:

http://mlg.ucd.ie/modules/COMP41680/assignment2/month-jan-001.html
12
<class 'bs4.element.ResultSet'>
[<a class="list-group-item list-group-item-action" href="month-jan-001.html"> January 2020  [1522]</a>, <a class="list-group-item list-group-item-action" href="month-feb-001.html"> February 2020  [1492]</a>, <a class="list-group-item list-group-item-action" href="month-mar-001.html"> March 2020  [1395]</a>, <a class="list-group-item list-group-item-action" href="month-apr-001.html"> April 2020  [1209]</a>, <a class="list-group-item list-group-item-action" href="month-may-001.html"> May 2020  [1315]</a>, <a class="list-group-item list-group-item-action" href="month-jun-001.html"> June 2020  [1376]</a>, <a class="list-group-item list-group-item-action" href="month-jul-001.html"> July 2020  [1358]</a>, <a class="list-group-item list-group-item-action" href="month-aug-001.html"> August 2020  [1254]</a>, <a class="list-group-item list-group-item-action" href="month-sep-001.html"> September 2020  [1347]</a>, <a class="list-group-item list-group-item-action" href="month-oct-001.html"> October 2020  [1395]</a>, <a class="list-group-item list-group-item-action" href="month-nov-001.html"> November 2020  [1343]</a>, <a class="list-group-item list-group-item-action" href="month-dec-001.html"> December 2020  [1220]</a>]

如果需要,对视图的响应:

endpoint='http://mlg.ucd.ie/modules/COMP41680/assignment2'
response = urllib.request.urlopen(endpoint)
text = response.read().decode()
print(text)

如何打印.list-group元素中所有锚元素的所有href属性?

【问题讨论】:

  • 您是在问如何从.list-group 元素中的所有锚元素中打印所有href 属性?
  • 是的,这正是我想要的

标签:
python
html
resultset