Python Forum
NameError: name 'u1' is not defined (on parser code Python)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NameError: name 'u1' is not defined (on parser code Python)
#1
hello. I test a code, and it's not working:

from bs4 import BeautifulSoup
import bs4
import requests
import os
import re

def getHTMLText(url):
	try:
		headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36'}
		
		r = requests.get(url, headers=headers)
		r.raise_for_status()
		r.encoding = r.apparent_encoding
		return r.text
	except:
		return "NO"
	
def main():
        for i in range(100):
                url = 'https://movie.douban.com/subject/30362186/comments?start'
                url = url +str(20*i)
                html = getHTMLText(url)
                soup = BeautifulSoup(html, 'html.parser')
                for new in soup.select('.comments'):
                        for b in new.select('a'):
                                u1=b.text
                        for c in new.select('.short'):
                                u2=c.text
			
print(u1+u2)
		
main()
Error:
Traceback (most recent call last): File "C:\Folder2\html parser.py", line 32, in <module> print(u1+u2) NameError: name 'u1' is not defined
Reply
#2
Most have print() in main() function.
There are serval other problem if i do a test,u1,u2 will have no output.
select('.comments') don't find anything the selectors has to more specific.
Loop for just give same page,just some fixes to get some output.
from bs4 import BeautifulSoup
import bs4
import requests
import os
import re

def getHTMLText(url):
    try:
        headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
        }
        r = requests.get(url, headers=headers)
        r.raise_for_status()
        #r.encoding = r.apparent_encoding
        return r.content # No text let bs4 handle it
    except:
        return "NO"

def main():
    for i in range(2):
        url = "https://movie.douban.com/subject/30362186/comments?start"
        url = url + str(20 * i)
        #print(url)
        html = getHTMLText(url)
        soup = BeautifulSoup(html, "html.parser")
        print(soup.select('#comments > div:nth-child(1) > div.comment'))
        '''for new in soup.select(".comments"):
            for b in new.select("a"):
                u1 = b.text
            for c in new.select(".short"):
                u2 = c.text
            print(u1 + u2)'''

main()
So the url generate,this for me just point to same page
https://movie.douban.com/subject/30362186/comments?start0
https://movie.douban.com/subject/30362186/comments?start20
A common mistake is to make loop or other stuff before has tested that stuff actually work on one page.
Then small step test next page and short loop,not 100 pages🌊
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  NameError issue with daughter's newb code MrGonk 2 182 Sep-16-2021, 01:29 PM
Last Post: BashBedlam
  NameError: name 'Particle' is not defined in Pygame drunkenneo 4 358 Aug-15-2021, 06:12 PM
Last Post: bowlofred
  I am getting a NameError that is not defined and not sure why it happen rick0922 5 592 Jun-14-2021, 03:41 PM
Last Post: deanhystad
  [split] NameError: name 'start_with_first_selected' is not defined knoxvilles_joker 4 781 Apr-18-2021, 07:59 PM
Last Post: knoxvilles_joker
  “NameError” “is not defined” script that I know works on other computers iamwithstupid 0 699 Nov-24-2020, 04:23 PM
Last Post: iamwithstupid
  NameError: name 'L2' is not defined soshelby 5 1,151 Nov-05-2020, 08:50 PM
Last Post: soshelby
  NameError: name 'target_id' is not defined bhagyashree 7 1,103 Nov-03-2020, 10:22 AM
Last Post: bhagyashree
  code not working, NameError: name 's' is not defined ridgerunnersjw 4 1,160 Oct-05-2020, 07:03 PM
Last Post: buran
  Going thru tutorials..."NameError: name 'raw_input' is not defined" hmonnier 4 1,395 Jul-14-2020, 02:19 PM
Last Post: BitPythoner
  NameError: name 'pins' is not defined glennford49 2 1,737 Jun-15-2020, 12:20 PM
Last Post: glennford49

Forum Jump:

User Panel Messages

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