Home > other >  TypeError: 'NoneType' object is not iterable error message
TypeError: 'NoneType' object is not iterable error message

Time:09-27

The from bs4 import BeautifulSoup
The import requests
The import sys
The import re

The class downloader (object) :
Def __init__ (self) :
The self. The server='http://www.biqukan.com/'
The self. The target='http://book.zongheng.com/showchapter/881459.html'
Self. Names=[] # deposit chapter
Self. Urls=[] # deposit section link
The self. The nums=0

# get a download link
Def get_download_url (self) :
The req=requests. Get (url=self. Target) # access page
Bf=BeautifulSoup (the req. Content, 'LXML') # parse page
Texts=bf. Find (' div 'class_="container") # lookup page
All_a=texts. Find_all (' a ')
Del all_a [0:5]
Self. Nums=len (all_a)
For x in all_a:
Self. Names. Append (x.s tring)
Self. Urls. Append (x.g et (' href '))
# print (x.s tring, x.g et (' href '))

# download chapter content
Def get_contents (self, target) :
The req=requests. Get (url=self. Target)
Bf=BeautifulSoup (the req. Content, 'LXML')
Texts=bf. Find (' div 'class_="content")
Return texts

# written to the file
Def writer (self, name, path, text) :
Write_flag=True
With the open (path, 'a', encoding="utf-8") as f:
F.w rite (name + '\ n')
F.w ritelines (text)
F.w rite (' \ n \ n ')


If __name__=="__main__" :
Dl=downloader ()
Dl. Get_download_url ()
Print (' "star super hunters" to start the download:)
Path=r 'D: \ \ star super hunters. TXT'
For I in range (dl. Nums) :
Dl. Writer (dl) names [I], path, dl. Get_contents (dl) urls [I]))
Sys. Stdout. Write (" downloaded: % 3 f "% % % float (I/dl. Nums) + '\ r')
Sys. Stdout. Flush ()
Print (' "star super hunters" the download is complete)

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
"Star super hunters" to start the download:
Traceback (the most recent call last) :
The File "D: \ A_python_work \ test. Py", line 49, in & lt; module>
Dl. Writer (dl) names [I], path, dl. Get_contents (dl) urls [I]))
The File "D: \ A_python_work \ test. Py", line 39, the writer in
F.w ritelines (text)
TypeError: 'NoneType' object is not iterable

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Source code as above with the result, prompt type error, which friend of hope?

CodePudding user response:

Ask answer wrong type is text format has a problem, instead of text=STR (text)
  • Related