【发布时间】:2017-07-19 05:54:32
【问题描述】:
我正在创建一个程序来从页面导航栏中获取最大的数字。我非常接近完成程序。但是因为我刚开始第一次编码 3 天,我无法找到这里的缺陷。
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 →
【问题讨论】:
-
在
bs = soup(html, "html.parser")行中,html不知从何而来,你在调用它之前没有定义它,所以你得到了错误name 'html' is not defined -
@OferSadan 你是对的。投票结束。
-
我不会投票关闭...这是一个合理的问题
标签: python python-3.x beautifulsoup urllib