Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am able to print the content of any website but im not able to write that content to a file.
Here, i want to write the content(i.e., text in this) to a file named key.txt
Please help! This is for my project!!

What I have tried:

from urllib.request import urlopen
import re
from bs4 import BeautifulSoup

url = input("enter full url:")
pattern = re.compile("[\w]+.[\w]+")
matchobject = pattern.match(url)
if (matchobject):
    print("your website is:",url)
    html =urlopen(url).read().decode()
    soup = BeautifulSoup(html,"html.parser")
    for script in soup(["script", "style"]):
       script.extract()
       text = soup.get_text()
       print(text)
       with open("C:\\Users\\Aishwarya\\Desktop\\key.txt", "w") as text_file:
        text_file.write(text)  # i have doubt over here
      
        text_file.close()
Posted
Updated 21-Jun-18 19:41pm

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900