【发布时间】:2015-04-19 18:18:51
【问题描述】:
我的代码:
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 提供服务:目的,但我想清理它并将其归结为这两个。好吧,显然不是因为它不起作用。
没有错误!
【问题讨论】:
标签: python parsing for-loop printing beautifulsoup