Python / beautifulsoup4:如何在一个类中存储来自两个不同的for循环的值?

时间:2018-09-08 15:04:23

标签: python web-crawler

该类如下,并具有4个值:

class CrawledInfo():
    def __init__(self, forward, repeat, like, holder, content):
        self.forward = forward
        self.repeat = repeat
        self.like = like
        self.holder = holder
        self.content = content`

在以后的函数中,我分别从2个if循环中获得了3个值和2个值。

soup = bs(doc, "html.parser")    
for i1 in soup.select(".WB_handle"):
    forward = i1.find(class_="W_ficon ficon_forward S_ficon").next_sibling.text
    repeat = i1.find(class_="W_ficon ficon_repeat S_ficon").next_sibling.text
    like = i1.find(class_="W_ficon ficon_praised S_txt2").next_sibling.text

for i in soup.select(".WB_detail"):
    holder = i.select_one(".WB_info .W_f14").text.strip()
    content = i.select_one(".WB_text").text.strip()

我想做的是按照以下顺序打印Class [转发,重复,持有人,内容* 1
转发,重复,喜欢,持有人,内容* 2 转发,重复,喜欢,持有人,内容* 3 ...]我遇到的问题是,在第一个“ if循环”中,我不能只在Class中存储三个值。

感谢所有帮助!

0 个答案:

没有答案