Simple code questions about Beautiful Soup...

Asked 2 years ago, Updated 2 years ago, 85 views


 import requests, bs4
 res = requests.get('http://tuportaldesbloqueo.com/')
 res.raise_for_status()
 soup = bs4.BeautifulSoup(res.text, "html.parser")
 elmes = soup.select('title')

 for elem in elmes:                                    
     print(elmes)

(j16134uk) EUNHYUKui-MacBook-Pro:tuportaldesbloqueo.com eunhyulkim$ python output1.py 
[<title>Davivienda</title>]

This is the current program

I just want to get the contents without the tag of the [<title></title>] part in the print result value,

elmes = elmes.replace("<title>","") I thought I should get rid of it like this, so I tried it

AttributeError: 'list' object has no attribute 'replace'

I got an error.

What should I do in this case?

python beautifulsoup

2022-09-22 18:47

2 Answers

If elmess is a list as shown below, you can separate the items into , , or all elements into a single string as shown below.

Removing tags can be removed with regular expressions.

In [1]: v = ["<title>Davivienda</title>"]

In [2]: ''.join(v)
Out[2]: '<title>Davivienda</title>'

In [3]: import re

In [4]: re.sub('<.+?>', '', ''.join(v)).strip()
Out[4]: 'Davivienda'

By the way, the select result of bs4 is bs4.element.Tag list, so it is better to use the text attribute.

In [9]: import requests, bs4
   ...: ...: res = requests.get('http://tuportaldesbloqueo.com/')
   ...: ...: res.raise_for_status()
   ...: ...: soup = bs4.BeautifulSoup(res.text, "html.parser")
   ...: ...: elmes = soup.select('title')

In [10]: elmes
Out[10]: [<title>Davivienda</title>]

In [11]: elmes[0].text
Out[11]: 'Davivienda'

In [12]: type(elmes[0])
Out[12]: bs4.element.Tag


2022-09-22 18:47

import requests, bs4
res = requests.get('http://tuportaldesbloqueo.com/')
res.raise_for_status()
soup = bs4.BeautifulSoup(res.text, "html.parser")
title = soup.select_one('title').text
print(title)

If you're just extracting the title of the page, that is, the Title.
Because I know to extract one element If you select a single target with select_one and extract it, you don't have to get data as a List object.


2022-09-22 18:47

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.