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)
#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


Messages In This Thread
RE: NameError: name 'u1' is not defined (on parser code Python) - by snippsat - Jul-26-2021, 04:36 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  I'm getting a NameError: ...not defined. vonArre 2 283 Mar-24-2024, 10:25 PM
Last Post: vonArre
  I want to create one resume parser api using Python Flask parthsukhadiya 1 849 Nov-28-2023, 05:07 PM
Last Post: Pedroski55
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 586 Nov-23-2023, 02:53 PM
Last Post: rob101
  Badly defined python code? Ken76 2 618 May-25-2023, 11:05 PM
Last Post: DigiGod
  Getting NameError for a function that is defined JonWayn 2 1,107 Dec-11-2022, 01:53 PM
Last Post: JonWayn
Question Help with function - encryption - messages - NameError: name 'message' is not defined MrKnd94 4 2,891 Nov-11-2022, 09:03 PM
Last Post: deanhystad
  [split] NameError: name 'csvwriter' is not defined. Did you mean: 'writer'? cathy12 4 3,326 Sep-01-2022, 07:41 PM
Last Post: deanhystad
  NameError: name ‘app_ctrl’ is not defined 3lnyn0 0 1,515 Jul-04-2022, 08:08 PM
Last Post: 3lnyn0
  NameError: name 'hash_value_x_t' is not defined Anldra12 5 1,921 May-13-2022, 03:37 PM
Last Post: deanhystad
  NameError: name 'cross_validation' is not defined tmhsa 6 13,352 Jan-17-2022, 09:53 PM
Last Post: TropicalHeat

Forum Jump:

User Panel Messages

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