Home > other >  <class 'list'> -. Find_all () returns the result of how to use
<class 'list'> -. Find_all () returns the result of how to use

Time:09-23


Here. Find_all () method returns the results, type is & lt; The class 'list' & gt;
When I use string to its method to the seven lines list,
So that I can't bring back three space of list elements are deleted
Tried also can't use the eval () function on the value (I don't want to remove the content to take out)
I try to write this variable to the local documents, after operation is not an error but documents didn't write anything in
Look at elder advice

 

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

Url="http://www.xinhuanet.com/fortune/"
Kv={' the user-agent ':' Mozilla/5.0}
R=requests. The get url, headers=(kv)
R.e ncoding="utf-8"
HTML=r.t ext
Soup=BeautifulSoup (HTML, "HTML parser")
For p in soup. The find (attrs={' class ':' swiper - wrapper '}). Children:
If isinstance (p, bs4. Element. The Tag) :
# print (p)
A=p.f ind_all (' a ')
A=STR (a)
N=re. The.findall (r '[a-z] + ://[^ \ s] *', a)
N=list (n)
# print (len (n))
# print (n)
# print (type (n))
N=n] [: - 1 # removes the last element from the list
# print (n)
N=STR (n)
# n.s trip (" '"' "')
# print (type (n))
N=n [2] this
# print (n)
# # n=n.s plitlines () returns a contains a list of all the various elements as
# print (type (n))
Print (n)
With the open (" test. TXT ", "w", encoding="utf-8") as f:
F.w rite (n) # this sentence built-in file close function, don't need to write f. lose ()
  • Related