0

I got the following code, that should be used in another function, so I want to pass it using a variable

soup.find("div", {"class" : "article-entry text"}).text.replace('\n', "")

Using

textFormat = "soup.find("div", {"class" : "article-entry text"}).text.replace('\n', "")"

doesn't work obviously. Do I have to escape characters? How?

What would be the best way to execute the content of textFormat. Like so?

text = exec(textFormat)

Thanks!

6
  • It might be a good idea to explain in details what you are trying to archive with that. Commented Sep 3, 2017 at 8:56
  • I got a big function, that can be applied to different websites for scraping except for this expression. This is why I want to put everything in one function and only pass the scraping statement, which is different for every website Commented Sep 3, 2017 at 8:58
  • Then you should have one base class where the subclasses overwrite one function. Commented Sep 3, 2017 at 9:19
  • But the function I am talking about is around 60 lines of code. I got only two minor changes, so overwriting the whole function seems like not the best idea, right? Commented Sep 3, 2017 at 9:30
  • You don't overwrite the whole function. Just a class method that runs the code that changes. Commented Sep 3, 2017 at 9:34

3 Answers 3

1

Use lambda:

soup_find = lambda x,y: soup.find(x,y).text.replace('\n', '')
soup_find("div", {"class" : "article-entry text"})
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. Lamdba seems to be coming up on my journey to learn python pretty often. Any good resource to learn it for a beginner? Have a hard time understanding what this is doing..
@user2252633 lambda is one of the useful things, if u sure know how to use that) docs.python.org/3/tutorial/controlflow.html#lambda-expressions
1

You can wrap it in another function like this:

def textFormat():
    return soup.find("div", {"class" : "article-entry text"}).text.replace('\n', "")

Then use it like this:

text = textFormat()

If you want to pass it to another function:

def func(another_func):
    return another_func()

func(textFormat)

3 Comments

Question was about variable :) But this variant too usable for sure
@JeffersonHoup textFormat IS a variable and you can pass it to another function like this func(textFormat)
Yeah, all right. In my first post I mean: "U're just create a function", because that expression: text = textFormat() will return value of func, because of "()". After update all right ) nvm so
1

You need to escape quotes that your string is surrounded by. Moreover you need to use raw string, to escape other chars. So ...:

textFormat = r'soup.find("div", {"class" : "article-entry text"}).text.replace(\'\n\', "")' 

But if you need to apply function that is have partially fixed elements you should just use partial from functools, not this ad hoc with eval. Using partial you can fix common arguments and pass others that are not common on every call.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.