如何在每个元素中迭代?
import requests
from bs4 import BeautifulSoup
url = "https://bigpara.hurriyet.com.tr/borsa/hisse-fiyatlari/"
r = requests.get(url)
soup = BeautifulSoup(r.content,"lxml")
details = soup.find_all("div",attrs={"class":"tBody"})
#print(type(details))
for detail in details:
print("{} {}".format(
detail.a.string,
detail.find("li",attrs={"class":"cell004"}).text.strip()
))
我们需要将股票与美丽的小组一起拿走。代码仅迭代第一个元素。如何迭代每个人?
import requests
from bs4 import BeautifulSoup
url = "https://bigpara.hurriyet.com.tr/borsa/hisse-fiyatlari/"
r = requests.get(url)
soup = BeautifulSoup(r.content,"lxml")
details = soup.find_all("div",attrs={"class":"tBody"})
#print(type(details))
for detail in details:
print("{} {}".format(
detail.a.string,
detail.find("li",attrs={"class":"cell004"}).text.strip()
))
We need to take the stocks with BeautifulSoup. Code only iterates the first element. How to iterate each one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先找到所有
ul
元素,然后迭代其根据class
明智的以dict> dict1
使用键标题找到标题和值
现在您可以使用
pandas
模块使用pd.dataframe
方法将数据转换为数据框,并根据所需的输出获取数据:
First find all
ul
elements and iterate over it to find title and values according toclass
wise and append items todict1
with keytitle
Now you can use
pandas
module to transform data to DataFrame usingpd.DataFrame
method and get data according to requiredOutput: