AttributeError: NoneType object has no attribute replace_with

2024/11/15 15:13:28

I am getting the following error:

Traceback (most recent call last):File "2.py", line 22, in <module>i.string.replace_with(i.string.replace(u'\xa0', '-'))
AttributeError: 'NoneType' object has no attribute 'replace_with'

Some of the code

soup = bs4(open("test.html")) table = soup.find("table", {"color":"#fff"})for i in soup.find_all('small'):i.string.replace_with(i.string.replace(u'\xa0', '-'))  <--Line 22

It was working yesterday, but I had to reinstall Mint on another VM and I can't get that to work again. How can I fix that?

EDIT: This is all the code:

from bs4 import BeautifulSoup as bs4soup = bs4(open("test.html")) table = soup.find("table", {"color":"#fff"})for i in soup.find_all('small'):i.string.replace_with(i.string.replace(u'\xa0', '-'))#print soup
f = open("new.html", "w")
f.write(str(table))

This is the table in test.html:

<table color="#fff">
<tr>
<td><small><small>&nbsp;</small></small></td>
</tr></table>
Answer

According to the documentation, the .string attribute can return None if a tag "contains more than one thing".

This only means that one of the elements in you list (soup.find_all('small')) is a non-leaf element. For instance :

<small>Hello <em>Bobby</em></small>

For such an element, .string returns None because it contains an other element, and the behavior is not defined.

Your code doesn't work because you can't run None.replace(u'\xa0', '-').What you need to do is, inside your loop, test whether the iterated element has a defined .string member or not.


for i in soup.find_all('small'):if i.string :i.string.replace_with(i.string.replace(u'\xa0', '-'))

NB : this is a dirty workaround, it won't work if you have

<small>&nbsp <tag>something unrelated</tag></small>

https://en.xdnf.cn/q/119043.html

Related Q&A

How to expand out a Pyspark dataframe based on column?

How do I expand a dataframe based on column values? I intend to go from this dataframe:+---------+----------+----------+ |DEVICE_ID| MIN_DATE| MAX_DATE| +---------+----------+----------+ | 1|…

How can I trigger my python script to automatically run via a ping?

I wrote a script that recurses through a set of Cisco routers in a network, and gets traffic statistics. On the router itself, I have it ping to the loopback address of my host PC, after a traffic thre…

How do I make my bot delete a message when it contains a certain word?

Okay so Im trying to make a filter for my bot, but one that isnt too complicated. Ive got this:@bot.event async def on_message(ctx,message):if fuck in Message.content.lower:Message.delete()But it gives…

pyinstaller cant find package Tix

I am trying to create an executable with pyinstaller for a python script with tix from tkinter. The following script also demonstrates the error: from tkinter import * from tkinter import tixroot = ti…

form.validate_on_submit() doesnt work(nothing happen when I submit a form)

Im creating a posting blog function for social media website and Im stuck on a problem: when I click on the "Post" button(on create_post.html), nothing happens.In my blog_posts/views.py, when…

How to find determinant of matrix using python

New at python and rusty on linear Algebra. However, I am looking for guidance on the correct way to create a determinant from a matrix in python without using Numpy. Please see the snippet of code belo…

How do I pass variables around in Python?

I want to make a text-based fighting game, but in order to do so I need to use several functions and pass values around such as damage, weapons, and health.Please allow this code to be able to pass &qu…

How to compare an item within a list of list in python

I am a newbie to python and just learning things as I do my project and here I have a list of lists which I need to compare between the second and last column and get the output for the one which has t…

Make for loop execute parallely with Pandas columns

Please convert below code to execute parallel, Here Im trying to map nested dictionary with pandas column values. The below code works perfectly but consumes lot of time. Hence looking to parallelize t…

Pre-calculate Excel formulas when exporting data with python?

The code is pulling and then putting the excel formulas and not the calculated data of the formula. xxtab.write(8, 3, "=H9+I9")When this is read in and stored in that separate file, it is sto…