我的代碼:Python:從列表解析只打印最後一項,不是全部?
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行代碼以前的工作文件中的服務爲我:目的,但我想它清理乾淨,並把箱子放到這兩個。好吧,顯然不是因爲它不起作用。
雖然沒有錯誤!