Python:Parse from list只打印最后一项,而不是全部?

Python:Parse from list只打印最后一项,而不是全部?,python,parsing,for-loop,printing,beautifulsoup,Python,Parsing,For Loop,Printing,Beautifulsoup,我的代码: from urllib2 import urlopen from bs4 import BeautifulSoup url = "https://realpython.com/practice/profiles.html" html_page = urlopen(url) html_text = html_page.read() soup = BeautifulSoup(html_text) links = soup.find_all('a', href = True) f

我的代码:

from urllib2 import urlopen
from bs4 import BeautifulSoup

url = "https://realpython.com/practice/profiles.html"

html_page = urlopen(url)
html_text = html_page.read()

soup = BeautifulSoup(html_text)

links = soup.find_all('a', href = True)

files = []
base = "https://realpython.com/practice/"


def page_names():
    for a in links:
        files.append(base + a['href'])

page_names()

for i in files:
    all_page = urlopen(i)

all_text = all_page.read()
all_soup = BeautifulSoup(all_text)
print all_soup
解析的前半部分收集三个链接,后半部分应该打印出所有的html

遗憾的是,它只打印最后一个链接的html

可能是因为

for i in files:
    all_page = urlopen(i)
它以前使用了8行代码为文件中的for i:purpose服务,但我想把它清理干净,并把它归结到这两行。显然不是因为它不起作用


没有错误

在for循环中,您正在访问all_页面,这将在每个循环中覆盖它,因此它将只具有上一次迭代的值


如果你想让它为每一页打印所有内容,你可以将这3行缩进到for循环中,然后每次都会在循环中执行它们。

在for循环中,你正在为所有内容页添加内容,这将在每个循环中覆盖它,因此它只会有最后一次迭代的值


如果你想让它为每一页打印所有内容,你可以将这3行缩进到for循环中,然后每次循环都会执行它们。

这似乎是一个格式问题,你可能想在循环中打印它,对吗

for i in files:
    all_page = urlopen(i)
    all_text = all_page.read()
    all_soup = BeautifulSoup(all_text)
    print all_soup

这似乎是jsut的格式问题,您可能打算在循环中打印它,对吗

for i in files:
    all_page = urlopen(i)
    all_text = all_page.read()
    all_soup = BeautifulSoup(all_text)
    print all_soup

您只在循环中存储最后一个值,需要在循环中移动所有分配和打印:

for i in files:
    all_page = urlopen(i)
    all_text = all_page.read()
    all_soup = BeautifulSoup(all_text)
    print all_soup
如果要使用函数,我将传递参数并创建列表,否则可能会得到意外的输出:

def page_names(b,lnks):
    files = []
    for a in lnks:
        files.append(b + a['href'])
    return files


for i in page_names(base,links):
    all_page = urlopen(i)
    all_text = all_page.read()
    all_soup = BeautifulSoup(all_text)
    print all_s
然后,您的函数可以返回一个列表:

def page_names(b,lnks):
    return [b + a['href'] for a in lnks]

您只在循环中存储最后一个值,需要在循环中移动所有分配和打印:

for i in files:
    all_page = urlopen(i)
    all_text = all_page.read()
    all_soup = BeautifulSoup(all_text)
    print all_soup
如果要使用函数,我将传递参数并创建列表,否则可能会得到意外的输出:

def page_names(b,lnks):
    files = []
    for a in lnks:
        files.append(b + a['href'])
    return files


for i in page_names(base,links):
    all_page = urlopen(i)
    all_text = all_page.read()
    all_soup = BeautifulSoup(all_text)
    print all_s
然后,您的函数可以返回一个列表:

def page_names(b,lnks):
    return [b + a['href'] for a in lnks]