Answer the question
In order to leave comments, you need to log in
How to fix NameError: name 'html' is not defined?
Hello ! I have this code:
import requests
from bs4 import BeautifulSoup
import pymysql
def get_html(url):
r = requests.get(url)
return r.text
# < Сбор ссылок.
def get_links(html):
soup = BeautifulSoup(html, 'lxml')
links=soup.findAll(link_container_array[0],{link_container_array[1]:link_container_array[2]})
#print(resource_name)
#print(len(links))
for link_container in links:
a_tag = link_container .find("a")
# Если нашел
if a_tag:
link = resource_url+ a_tag.get("href")
#print(link)
# < Сбор контента.
def get_content(html,link):
soup = BeautifulSoup(html,'lxml')
headline=soup.find(title_array[0],{title_array[1]:title_array[2]})
print(headline)
#< Вызывание всех функций.
def get_all_func (resource_allnews_link):
url = resource_allnews_link
links = get_links(get_html(url))
content = get_content(html ,links)
# < Подключение к базе данных.
connection = pymysql.connect(host='localhost',
user='root',
password='',
db='news_portal',
charset='utf8',
autocommit=True)
cursor = connection.cursor()
# < Запрос правил выдергивания контента.
cursor.execute('SELECT `resource_id`, `resource_name`, `resource_url`, `resource_allnews_link`, `link_rule`, `mainblock`, `mainblock1`, `page_link`, `title`, `datetime`, `datetime1`, `text`, `text1` FROM `resource` WHERE 1')
resources=cursor.fetchall()
# < Цикл для перебора из кортежа.
for resource in resources:
resource_name=resource[1]
resource_url=resource[2]
resource_allnews_link=resource[3]
content_rule=resource[4]
title=resource[8]
resource_name_array=resource_name.split(',')
link_container_array=content_rule.split(',')
title_array=title.split(',')
get_all_func(resource_allnews_link,)
connection.close()
Traceback (most recent call last):
File "C:/Users/Administrator/PycharmProjects/Task/sql_pars.py", line 57, in
get_all_func(resource_allnews_link,)
File "C:/Users/Administrator/PycharmProjects/Task/sql_pars.py". py", line 33, in get_all_func
content = get_content(html ,links)
NameError: name 'html' is not defined
Process finished with exit code 1
Answer the question
In order to leave comments, you need to log in
Can you just declare this variable?
def get_all_func (resource_allnews_link):
url = resource_allnews_link
links = get_links(get_html(url))
content = get_content(html ,links)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question