我写了以下代码
page=requests.get("http://3.85.131.173:8000/random_company")
soup=BeautifulSoup(page.content,"html.parser")
info_list=soup.find_all("li")
print(info_list)
和print给出如下答案
[<li>Name: Walker, Meyer and Allen</li>, <li>CEO: David Pollard</li>, <li>CTO: Sandra Boyd</li>, <li>Address: 275 Jones Station Suite 008
Bradburgh, UT 24369</li>, <li>Investment Round: C</li>, <li>Purpose: Reduced logistical contingency for whiteboard end-to-end applications</li>]
我想提取名称和位置早些时候我使用索引,但它是动态的,谁能建议如何提取名称和目的。
反馈后的编辑代码:
page=requests.get("http://3.85.131.173:8000/random_company")
soup=BeautifulSoup(page.content,"html.parser")
info_list=soup.find_all("li")
print(info_list)
name=[]
purpose=[]
我现在能够成功打印姓名和位置。它给出如下输出['Name: Burnett and Sons']假设我只想要Burnett and Sons,那么我该怎么做?有什么建议吗?
我想你在找这样的东西:
targets = ["Name","Purpose"]
for item in info_list:
if item.text.split(":")[0] in targets:
print(item.text)
输出(在本例中):
Name: Jimenez LLC
Purpose: Mandatory context-sensitive approach for leverage compelling communities
if 'Name' in item.text:
name=name.append(item) <-- Wrong: assigns None to name
if 'Purpose' in item.text:
purpose=purpose.append(item) <-- Wrong: assign None to purpose
上面的两条尖线就是问题所在。list.append()
返回None。
(参见进一步解释:为什么Python中append()总是返回None ?)
要获得预期的输出,请删除name=
部分,并将list.append()
添加到列表中,如下所示:
for item in item_list:
if 'Name' in item.text:
name.append(item.text)
if 'Purpose' in item.text:
purpose.append(item.text)
print(name, purpose)
应该打印:
['Name: Ward and Sons'] ['Purpose: User-friendly mission-critical algorithm for visualize killer e-business']