Python 3.x:从页面导航部分找到最大的数字?
问题描述:
我正在创建一个程序来从页面导航栏中获取最大的数字。我非常接近完成该程序。但因为我刚开始第一次编码3天,所以无法找到缺陷在这里。Python 3.x:从页面导航部分找到最大的数字?
import bs4
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
#List for extended links to the base url
links = ['Link_1/','Link_2/','Link_3/']
#Function to find out the biggest number present in the page navigation
section.Every element before 'Next→' is consist of the upper limit
def page_no():
bs = soup(html, "html.parser")
max_page = bs.find('a',{'class':'next page-numbers'}).findPrevious().text
print(max_page)
#url loop
for url in links:
my_urls ='http://www.example.com/category/{}/'.format(url)
page_no()
# opening up connection,grabbing the page
uClient = uReq(my_urls)
page_html = uClient.read()
uClient.close()
错误: Traceback (most recent call last): line 20, in <module> page_no()
line 14, in page_no
bs = soup(html, "html.parser")
NameError: name 'html' is not defined`
我试着不创建一个函数来创建这个程序,但它只是回到我的最后一个元素的值从列表,而不是所有的数字。
困惑:
混淆元素def page_no()
,#Url Loop
和# opening up connection,grabbing the page
的序列(我可能是错的)提前
感谢。页面导航的
例子:
1 2 3 … 15 Next →
答
您正试图解析页面你有没有抓住它。
移动此行
page_no()
到最后,你抢到页之后。但即使如此,您将您的“抓取页面”命名为page_html
,但在page_no()
之内,您正在寻找其他名称html
。所以,你可以尝试改变过,更换
bs = soup(html, "html.parser")
与
bs = soup(page_html, "html.parser")
PS,你有一个像你的代码缩进问题的其他问题,你必须对他们太理清这工作
行'bs = soup(html,“html.parser”)''html'是从无处来的,你在调用它之前没有定义它,所以你得到错误'name 'html'没有定义' –
@OferSadan你说得对。投票结束。 –
我不会投票结束......这是一个合法的问题 –