I want to get a list of titles in this post
http://www.vn-meido.com/k1/index.php?topic=6646.msg38869#msg38869
and this is the code
from os import system
import requests
from bs4 import BeautifulSoup
list_url = "http://www.vn-meido.com/k1/index.php?topic=6646.msg38869#msg38869"
list_page = requests.get(list_url)
soup = BeautifulSoup(list_page.content,'html5lib')
titles = soup.find_all('div',{"class":"inner", "id":"msg_38869"})
title_list = []
for t in titles:
title = t.find('a').text
title_list.append(title)
print(title_list)
but it only get the first title in the post:
['10 nen goshi no HikiNiito o Yamete Gaishutsushitara Jitaku goto Isekai']
I know that there is something wrong with the loop but I don't know how to fix it.
CodePudding user response:
you can get all elements and then append their text to your list.
from os import system
import requests
from bs4 import BeautifulSoup
list_url = "http://www.vn-meido.com/k1/index.php?topic=6646.msg38869#msg38869"
list_page = requests.get(list_url)
soup = BeautifulSoup(list_page.content, 'html5lib')
titles = soup.find_all('div', {"class": "inner", "id": "msg_38869"})
title_list = []
for t in titles:
title = t.find_all('a')
for x in title:
title_list.append(x.text)
print(title_list)
