Python:将解析的信息返回到列表?
我的代码:Python:将解析的信息返回到列表?,python,parsing,for-loop,return,beautifulsoup,Python,Parsing,For Loop,Return,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 = []
def page_names():
for a in links:
files.append(a['href'])
return files
page_names()
print files[:]
base = "https://realpython.com/practice/"
print base + files[:]
我试图解析出三个网页文件名并将它们附加到“文件”列表中,然后以某种方式将它们附加或添加到基本url的末尾,以便进行简单打印
我曾尝试将“base”作为一个单独的项目列表,以便添加,但我对Python还是比较陌生,我认为我把for语句搞砸了
目前我得到:
print files[:]
TypeError: 'type' object has no attribute '__getitem__'
最后定义了
list[:]
,这是完全错误的,因为list
是用于创建实际列表的内置关键字
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 = []
def page_names():
for a in links:
files.append(a['href'])
page_names()
base = "https://realpython.com/practice/"
for i in files:
print base + i
输出:
https://realpython.com/practice/aphrodite.html
https://realpython.com/practice/poseidon.html
https://realpython.com/practice/dionysus.html
您不需要创建中间列表来存储链接或文件,只需使用列表即可
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)
files = [i['href'] for i in soup.find_all('a', href = True)]
base = "https://realpython.com/practice/"
for i in files:
print base + i
links=soup.find_all('a',href=True)
仍然是相同的错误,但谢谢。您尚未定义列表,因此它仍然是内置类型。你是说打印链接
(打印时不需要浅拷贝)?