python如何将页码作为变量

2024-09-27 23:17:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图取消一些电影评论和评级,我试图结束这段代码,如果没有更多的评论和评级。在

page = 1  #'variable' page
blankList = []
while True:
    url = "http://movie.naver.com/movie/bi/mi/pointWriteFormList.nhn?code=99715&type=after&isActualPointWriteExecute=false&isMileageSubscriptionAlready=false&isMileageSubscriptionReject=false&page={}".format(page) 
    res = requests.get(url)
    bs = BeautifulSoup(res.text, "lxml")
    valueList = bs.findAll("input",{'value':"{}"}).format(str(page))

    ###the upper code won't work when 'variable page' is bigger than 'maximum page'. if so, it returns blank List.

    page = page + 1
    score_result = bs.find("div", {'class': 'score_result'})
    for review_one in score_result.findAll('li'):
        score = int(review_one.find("div", {'class': 'star_score'}).find('em').text)
        reple = review_one.find('div',{'class': 'score_reple'}).find('p')
        for span in reple.findAll('span'):
            span.extract()
        reple = reple.text
        print("reple: {}, score: {}".format(reple, score))

    if (valueList == blankList):
        break

所以我的问题有点简单。你可能知道这部分行不通

^{pr2}$

假设最大页数是100,当我在variable页中输入20时

url = "http://movie.naver.com/movie/bi/mi/pointWriteFormList.nhn?code=99715&type=after&isActualPointWriteExecute=false&isMileageSubscriptionAlready=false&isMileageSubscriptionReject=false&page={}".format(page)

这个部分没有问题,当我试着在这里输入20时(真实页码)

^{pr2}$

它将返回此列表。在

['input id="page" name="page" type="hidden" value="20"/>]

但当我试着输入200时(两个都输入) 第一个代码不会显示错误,但是

^{pr2}$

它返回空白列表,因为没有第200页。在


我需要的是:

  1. 提取此列表中value的值,并将其设置为变量。 在这个单子里,我需要的是87。在

    ['input id="page" name="page" type="hidden" value="87"/>]

或者

  1. 有没有可能将变量放入findAll函数中?在

    • 当我运行这个代码时,这个列表会弹出 bs.findAll("input")

      ['input type="hidden" name="onlyActualPointYn" id="onlyActualPointYn" value="N">, 'input type="hidden" name="movieCode" id="movieCode" value="99715">, 'input type="hidden" name="order" id="order" value="sympathyScore">, 'input type="hidden" name="page" id="page" value="75">, 'input type="hidden" name="point" id="point" value="0">]


Tags: nameidfalseformatinputbsvaluetype
1条回答
网友
1楼 · 发布于 2024-09-27 23:17:39

请按以下所述更改线路

valueList = bs.findAll("input",{'value':"{}".format(str(page))})

此程序无法退出,因为每个页码都是此url的有效页码。如果您输入7777777,它也是一个有效的页码。你可以在浏览器里试试。在

输出:

^{pr2}$

相关问题 更多 >

    热门问题