虽然我似乎不是第一个遇到这个问题的人,但我还是没能找到问题的答案。在
我正在抓取一个HTML表,虽然我试图遍历它,但我只从表中获取第一行。在
import requests
from bs4 import BeautifulSoup
# Webpage connection
html = "https://www.wegochem.com/chemicals/organic-intermediates/supplier-distributor"
r=requests.get(html)
c=r.content
soup=BeautifulSoup(c,"html.parser")
# Grab title-artist classes and store in recordList
wegoList = soup.find_all("tbody")
try:
for items in wegoList:
material = items.find("td", {"class": "click_whole_cell",}).get_text().strip()
cas = items.find("td", {"class": "text-center",}).get_text().strip()
category = items.find("div", {"class": "text-content short-text",}).get_text().strip()
print(material,cas,category)
except:
pass
第一行的结果是正确的:(1,2-二甲基咪唑1739-84-0有机中间体、塑料、树脂和橡胶、涂料); 但是for循环并没有在表中循环。在
谢谢你的帮助
for items in wegoList:
循环遍历tbody
的列表,然后尝试从整个表中提取属性,但应该遍历tr
行:请尝试以下代码:
^{pr2}$输出:
相关问题 更多 >
编程相关推荐