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
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
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.
574 Who developed the "avformat-59.dll" that comes with FFmpeg?
578 Understanding How to Configure Google API Key
613 GDB gets version error when attempting to debug with the Presense SDK (IDE)
619 Uncaught (inpromise) Error on Electron: An object could not be cloned
916 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
© 2024 OneMinuteCode. All rights reserved.