Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with for loop
#1
Here we go, I using a text file with url in it, one per line. I am using a for loop to go one line at a time through the list. Then I am feeding that for loop into an email scraping
code. I get about the first url then an error.
Here is my code

import urllib.request, re

url_files = open("test.txt", "r")
lines = url_files.readlines()
for line in lines:
      
        text = urllib.request.urlopen(line).read().decode('utf-8')
        regex = re.compile(r'[\w.-]+@[\w.-]+')
        email = re.findall(regex, text)
        print(email)

url_files.close
Here is the error, you can see the emails from the first url, then the error

['[email protected]', '[email protected]', '[email protected]']
Traceback (most recent call last):
File "D:\Users\computer\Desktop\python use file\read_fil_and_use_for_loop.py", line 7, in <module>
text = urllib.request.urlopen(line).read().decode('utf-8')
File "D:\Python37\lib\urllib\request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "D:\Python37\lib\urllib\request.py", line 510, in open
req = Request(fullurl, data)
File "D:\Python37\lib\urllib\request.py", line 328, in __init__
self.full_url = url
File "D:\Python37\lib\urllib\request.py", line 354, in full_url
self._parse()
File "D:\Python37\lib\urllib\request.py", line 383, in _parse
raise ValueError("unknown url type: %r" % self.full_url)
ValueError: unknown url type: ''

I have just got in to using for loop, so I am not very good at them yet.
Hope some one can help.
renny
ps. the list

http://www.cityofalbionmi.gov/
http://www.revize.com
http://www.cityofalbionmi.gov/government.../index.php
http://www.cityofalbionmi.gov/government.../index.php
http://www.cityofalbionmi.gov/residents/.../index.php
http://www.cityofalbionmi.gov/government.../index.php
http://www.cityofalbionmi.gov/department...ration.php
http://www.cityofalbionmi.gov/department.../index.php
http://www.cityofalbionmi.gov/department.../index.php
http://www.cityofalbionmi.gov/department.../index.php
http://www.cityofalbionmi.gov/department.../index.php
http://www.cityofalbionmi.gov/department.../index.php
http://www.cityofalbionmi.gov/department.../index.php
http://www.cityofalbionmi.gov/department.../index.php
http://www.cityofalbionmi.gov/department.../index.php
http://www.cityofalbionmi.gov/department.../index.php
http://www.cityofalbionmi.gov/department.../index.php
http://www.cityofalbionmi.gov/department.../index.php
http://www.cityofalbionmi.gov/department.../index.php
http://www.cityofalbionmi.gov/department..._sewer.php
http://www.cityofalbionmi.gov/residents/index.php
http://albionmich.com/calendar/index.html
Reply


Messages In This Thread
Problem with for loop - by Blue Dog - Jul-09-2018, 03:21 PM
RE: Peoblem with for loop - by buran - Jul-09-2018, 04:27 PM
RE: Problem with for loop - by Blue Dog - Jul-09-2018, 05:33 PM
RE: Problem with for loop - by buran - Jul-09-2018, 05:35 PM
RE: Problem with for loop - by Blue Dog - Jul-09-2018, 05:43 PM
RE: Problem with for loop - by buran - Jul-09-2018, 06:46 PM
RE: Problem with for loop - by Blue Dog - Jul-09-2018, 07:07 PM
RE: Problem with for loop - by nilamo - Jul-09-2018, 07:58 PM
RE: Problem with for loop - by Blue Dog - Jul-10-2018, 01:31 AM
RE: Problem with for loop - by buran - Jul-10-2018, 05:50 AM
RE: Problem with for loop - by Blue Dog - Jul-10-2018, 07:13 PM
RE: Problem with for loop - by Blue Dog - Jul-10-2018, 11:21 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020