我正在使用python尝试我的第一个小web抓取尝试,我遇到了以下问题:
for resultat in tr.find_all(class_='tc fs-17 white bg-darkgrey p-r' or class_='tc fs-9 white bg-red mb-2 lh-data'):
data.append(resultat.text) 我需要确保在for循环中,class_是数据被附加到数据。但是,我不知道该怎么做。
帮点忙就好了。
致以敬意,
发布于 2020-05-29 21:14:42
为了简单起见,您可以分离循环。
for resultat in tr.find_all(class_='tc fs-17 white bg-darkgrey p-r'):
data.append(resultat.text)
for resultat in tr.find_all(class_='tc fs-9 white bg-red mb-2 lh-data'):
data.append(resultat.text)发布于 2021-02-11 12:42:46
如果您想使用一个循环,可以首先使用.extend method将一个列表附加到另一个列表中。
background_darkgrey = tr.find_all(class_='tc fs-17 white bg-darkgrey p-r')
background_red = tr.find_all(class_='tc fs-9 white bg-red mb-2 lh-data')
elements_to_scrape = background_darkgrey.extend(background_red)然后,只需在elements_to_scrape上进行迭代。
for resultat in elements_to_scrape:
data.append(resultat.text)https://stackoverflow.com/questions/62094516
复制相似问题