【发布时间】:2021-10-25 23:08:05
【问题描述】:
我写了以下代码
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)
并打印给出以下答案
[<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 那么我应该怎么做?有什么建议吗?
【问题讨论】:
-
遍历列表 (
for item in info_list:) 并检查if "Name" in item:,同样为位置做 -
你想提取所有名字和他们各自的位置吗?还是只有标记为
'Name'的项目? -
你是“要提取姓名和职位”还是“要提取姓名和目的”?
-
目前还不清楚你到底想刮什么。请edit您的问题以显示预期的输出。
-
@matiiss-我想提取名称和目的。我尝试了下面的代码 { page=requests.get("3.85.131.173:8000/random_company") soup=BeautifulSoup(page.content,"html.parser") info_list =soup.find_all("li") for item in info_list: if("Name" in item): print(item) } 我只得到一个目的输出。我需要名称和目的。我也循环了名称但它给了我输出作为目的?
标签: python html web-scraping beautifulsoup