When I am doing a homework problem or self-studying, I can usually do proofs that only require theorems or definitions. But lately I've been trying to read more advanced texts in analysis and algebra on my own and its clear to me that I cannot solve problems just by using the definitions or theorems. For example:
Let $E'$ be the set of all limit points of a set $E$. Prove that $E'$ is closed.
In fact after spending weeks on this problem, I decided to look up the solution. Although I understand the solution, I don't think I could have ever come up with such a solution to the problem.
Is struggling like this for weeks the best way to solve more difficult problems? If not, what should I do differently?

Surely, it is better to spend some time thinking about a theorem or a lemma before reading the proof directly. Otherwise, you might not appreciate the value of the proof. Moreover, you might be able to come up with the proof before reading the solution, maybe using a different approach.
However, if you try to prove something and you keep using the same approach and failing. Then this might be the time to do something different. In addition, if you are learning and you get stuck at a proof for weeks then you are missing the oppurtunity to learn something new during this time. I usually try to prove theorems of a math book before looking at the proof given in the book. On average, I allow about one hour after which if I still fail, I look at the book's proof.
A nice book about problem solving skills (it is mostly about high school math though) is the "Art and Craft of Problem solving"