Can't import request

So in real Python this code works:


from urllib.request import Request, urlopen

url="https://svnweb.freebsd.org/csrg/share/dict/words?revision=61569&view=co"
req = Request(url, headers={'User-Agent': 'Mozilla/5.0'})

web_byte = urlopen(req).read().decode('utf-8')

word_list = web_byte.split()

But in Revit 21 I get : ImportError: No module named request

So I’m guessing IronPython says no.

I want to generate a random word. Any solutions or do I have to wait for Revit 23?

It’s there since the invention of text to speak (like Siri), or even before that.
You choose a random youtube video, extract the audio and translate it into written text. (Google it)
Use String.Split node in Dynamo and use the space character as the “splitter”
Then use a Randomizer workflow.

Well I got round it by converting the webpage into a text file and doing this:

with open(‘…\words.txt’) as f:
word_list = f.readlines()

Ok, my solution could be in the category “fun to try” :slight_smile:

1 Like

Oh for sure!! I will :slight_smile:
Thanks!

Soo apparently with Python rubbish you have to do this:

image

1 Like

Hello,
try using urllib2 library with IronPython2.7

also, you can to use .Net (work both engines)

import clr
import sys
import System
from System.IO import *
from System.Net import *

url="https://svnweb.freebsd.org/csrg/share/dict/words?revision=61569&view=co"

request = WebRequest.Create(url)
with request.GetResponse() as response:	 
	with response.GetResponseStream() as stream:
		with StreamReader(stream) as reader:
			html = reader.ReadToEnd()
			#html = html.encode("utf-8")

lstTxt = html.split()

OUT = lstTxt
3 Likes