1

I am using a python3.x script to save a string to a text file:

nN = "hello"

f = open("file.txt", "w")
f.write(nN)
f.close()

and now I am trying to parse the content of an h2 element from a website (page scraping works fine) and I am getting an error when I am trying this:

nN = driver.find_element_by_id("title")

f = open("file.txt", "w")
f.write(nN)
f.close()

where the html line is:

<h2 id="title">hello</h2>

The error is:

write() argument must be str, not WebElement

I tried converting the nN into a string using the following:

f.write(str(nN))

and the new error is:

invalid syntax

1
  • How about f.write(str(nN))? I'm not sure what a driver object is, or what find_element_by_id() returns; but as the error states, f.write() must have a type str as its explicit parameter. Commented Oct 5, 2017 at 14:40

1 Answer 1

1

It looks like you are using Selenium and then using the webdriver to parse the html content?

The reason the string conversion is not working is because the nN is a Selenium/html object that probably is a dictionary or a list. You could try simply f.write(nN.text) and according to the documentation the .text version of nN should work.

To the larger issue of parsing html though, I would recommend using Beautiful Soup. Do pip3 install BeautifulSoup4 and then to import from bs4 import BeautifulSoup. Then as example:

with open('file.html','r') as f:
  htmltext = f # change as necessary, just needs to be string
  soup = BeautifulSoup(htmltext,'lxml')
  h2found = soup.find('h2',id="title")
  print(h2found)
  print(h2found.text)

Beautiful Soup has great documentation and is the standard and best library to use for parsing html.

Sign up to request clarification or add additional context in comments.

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.