解析器BS4写入文件

时间:2019-05-08 16:04:27

标签: python web-scraping beautifulsoup

我正在研究python并有一项任务。 我必须将抓取结果写入文件,但是有一些错误。结果文件中的每个字符串都为“ None”,但是当我将这些结果输出到控制台时,它正常工作。

在takeText函数中

def takeText(self,nameArticleFile):
        x=input("Введите слово по которому нужно отыскать статьи\n")
        for titlesearch in self.soup1.findAll('item'):
            if titlesearch.find(string=re.compile(x)):
                self.zagsearch.append(titlesearch.find(string=re.compile(x)))
                self.urlsearch.append(titlesearch.find('guid',text=True))
        print('Статьи в которых есть вхождение слова:')
        for s in range(len(self.zagsearch)):
            print(self.zagsearch[s].string)
        print('Ссылки на статьи в которых найдено слово:')
        for a in range(len(self.urlsearch)):
            print(self.urlsearch[a].string)
        for b in range(len(self.urlsearch)):
            self.response1=urllib.request.urlopen(self.urlsearch[b].string)
            self.soup2=BeautifulSoup(self.response1.read())
            with open(nameArticleFile, "w") as file:
                for i in self.soup2.findAll('p'):
                    file.write(str(self.searchP.append(i)))
            for i in range(len(self.searchP)):
                print(self.searchP[i].string)

1 个答案:

答案 0 :(得分:0)

append()始终返回None,因此write(...append())表示write(None)

您必须写i(或i.string),而不是append(i)

with open(nameArticleFile, "w") as file:
    for i in self.soup2.findAll('p'):

        # display and write `i`
        print(i.string)
        file.write(i.string)

        # add to list
        self.searchP.append(i)

        # or  display and write last item from list
        #print(self.searchP[-1].string)
        #file.write(self.searchP[-1].string)

在Python中,我们使用

而不是range(len(self.searchP))
for item in self.searchP:
     print(items.string)