-2

I want to print the result of the Python code to a text file. How can I do that? I want to write print output to text file. Can you help me please?

import requests
from bs4 import BeautifulSoup
import re

query = "data science"
search = query.replace(' ', '+')
results = 10
url = (f"https://www.google.com/search?q={search}&num={results}")

requests_results = requests.get(url)
soup_link = BeautifulSoup(requests_results.content, "html.parser")
links = soup_link.find_all("a")

for link in links:
    link_href = link.get('href')
    if "url?q=" in link_href and not "webcache" in link_href:
      title = link.find_all('h3')
      if len(title) > 0:
          print(link.get('href').split("?q=")[1].split("&sa=U")[0])
          print(title[0].getText())
          print("------")
Fuat Yavuz
  • 11
  • 2

1 Answers1

0

you have to open a file in write mode and do the write operation in that file.

f = open('file', 'rb+') f.write(b'0123456789abcdef') f.close