Python 2.7 为什么靓汤不按顺序返回结果?

Python 2.7 为什么靓汤不按顺序返回结果?,python-2.7,web-scraping,beautifulsoup,Python 2.7,Web Scraping,Beautifulsoup,我想从IEEE explorer中提取论文和作者的列表。现在的问题是,下面的代码首先返回作者的姓名,然后返回论文列表,但在网页中,内容的顺序与此不同。我不知道为什么会这样 import requests page = requests.get('http://ieeexplore.ieee.org/xpl/mostRecentIssue.jsp?punumber=7109453&rowsPerPage=100') import bs4 soup = bs4.BeautifulSoup(

我想从IEEE explorer中提取论文和作者的列表。现在的问题是,下面的代码首先返回作者的姓名,然后返回论文列表,但在网页中,内容的顺序与此不同。我不知道为什么会这样

import requests
page = requests.get('http://ieeexplore.ieee.org/xpl/mostRecentIssue.jsp?punumber=7109453&rowsPerPage=100')

import bs4
soup = bs4.BeautifulSoup(page.content)
names = soup.select('.prefNameLink , .art-abs-url span')

for index, name in enumerate(names):
    print name.text.strip()
使用like
.prefNameLink.art abs url span
时,所选元素的顺序不能保证与它们在文档中的显示顺序相匹配。如示例所示,首先选择所有
.prefNameLink
元素,然后再次解析整个文档,并选择所有
.art abs url span
元素

解决此问题的最简单方法是迭代每个容器元素,然后检索该容器中的每个对应名称

在执行此操作时,仅对文档进行一次解析,并维护每个组的顺序:

for container in soup.select('.results > li'):
    for name in container.select('.prefNameLink, .art-abs-url span'):
        print name.text.strip()
然而,由于每篇论文可能有多个作者,更好的方法是对每篇论文的每个作者进行迭代

这样做时,订单也将在每个容器内维护:

for container in soup.select('.results > li'):
    for paper in container.select('.art-abs-url span'):
        print paper.text.strip()

        for author in container.select('.prefNameLink'):
            print author.text.strip()