只返回第一项

2024-09-30 08:15:35 发布

您现在位置:Python中文网/ 问答频道 /正文

虽然我似乎不是第一个遇到这个问题的人,但我还是没能找到问题的答案。在

我正在抓取一个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循环并没有在表中循环。在

谢谢你的帮助


Tags: textinimportforgethtmlitemscontent
2条回答

for items in wegoList:循环遍历tbody的列表,然后尝试从整个表中提取属性,但应该遍历tr行:

wegoList = soup.find_all("tbody")

try:
    soup=BeautifulSoup(wegoList.__str__(),"html.parser")
    trs = soup.find_all('tr') #Makes list of rows

    for tr in trs: 
        material = tr.find("td", {"class": "click_whole_cell",}).get_text().strip()

        cas = tr.find("td", {"class": "text-center",}).get_text().strip()

        category = tr.find("div", {"class": "text-content short-text",}).get_text().strip()

    print(material,cas,category)

请尝试以下代码:

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_all("td", {"class": "click_whole_cell",})
        for i in material:
            print(i.get_text().strip())

        cas = items.find_all("td", {"class": "text-center",})
        for i in cas:
            print(i.get_text().strip())

        category = items.find_all("div", {"class": "text-content short-text",})
        for i in category:
            print(i.get_text().strip())

except:
    pass

Updated code:

^{pr2}$

输出:

1,2-Dimethylimidazole 1739-84-0 Organic Intermediates, Plastic, Resin & Rubber, Coatings
1,6-Hexanediol 629-11-8 Adhesives & Sealants, Industrial Chemicals, Inks & Digital Inks, Organic Intermediates, Plastic, Resin & Rubber, Coatings
2,2,4-Trimethyl-1,3-Pentanediol Monoisobutyrate 25265-77-4 Inks & Digital Inks, Oil Field Services, Organic Intermediates, Solvents & Degreasers, Coatings
2,6-Dichloroaniline 608-31-1 Agricultural Chemicals, Crop Protection, Organic Intermediates

相关问题 更多 >

    热门问题