【问题标题】:list for a Python web application malfunctioningPython Web 应用程序出现故障的列表
【发布时间】:2018-09-02 05:22:03
【问题描述】:

我需要一些关于我正在为我的 coursera 课程编写的代码的帮助,目标如下:在第 18 位找到链接(名字是 1)。按照那个链接。重复此过程 7 次。答案是您检索到的姓氏。 提示:您将加载的最后一页名称的第一个字符是:J from link:(http://py4e-data.dr-chuck.net/known_by_Shannon.html)

我已经为此任务编写了代码,但它似乎只适用于第一个项目,并且自第一个项目以来的每个站点,代码列表都出现故障。我的想法是获取 Html 代码并将 url 附加到列表中,然后从列表中找到第 18 项,然后使用新 url 重定向整个循环并删除旧列表。重复该过程7次。我对代码到底在哪里出错感到非常困惑。提前致谢。

import urllib.request, urllib.parse, urllib.error
from bs4 import BeautifulSoup
import ssl
import re
term_counter = (0)
file = list()
regex = list()
# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

url = input('Enter - ')
for I in range(7) :
    html = urllib.request.urlopen(url, context=ctx).read()
    soup = BeautifulSoup(html, 'html.parser')
    tags = soup('a')
    del file[:]
    file = list()
    for tag in tags :
        file.append(tag)
        print(tag.contents[0])
        url = tag.get('href')
        print (url)
    for items in range(17,18) :
        print(file[items])

我是该网站的新手,我不确定这是否是提出 python 问题的正确位置,如果不是,请通知我,我会将其重新发布到正确的位置。

【问题讨论】:

  • range(17,18) 看起来很奇怪,它与 print(file[17]) 相同,因为 range(17, 18) 只产生一个数字
  • del file[:]; file = list()file[:] 相同,与file = list() 相同。 Python 只是简单地将新对象赋予名称file,无需删除之前的值。此外,由于您实际上并未在循环中使用 I,因此习惯上使用下划线 _I 来表示。

标签: python python-3.x web-scraping python-webbrowser


【解决方案1】:

你可以试试这个:

import bs4
import requests

url = 'http://py4e-data.dr-chuck.net/known_by_Shannon.html'

for I in range(7) :
    session = requests.get(url)
    soup = bs4.BeautifulSoup(session.text, "html.parser")
    target_list = soup.findAll("li")[17]
    href = target_list.find("a").get("href")
    link_name = target_list.find("a").text

    if(I == 6):
        print('Solution is ' + link_name)
    else:
        print('Name is: ' + link_name)
        print('Going to web is: ' + href)
        url = href

答案将是最后打印的链接名称。

编辑:

【讨论】:

    【解决方案2】:

    问题在于,尽管您打印出第 18 个 tags 条目,但您并未将 url 设置为该值。不幸的是,您在tags 循环中也使用了url,因此您不会注意到这个错误。在您的代码中,url 仍设置为tags 的最后一个条目。如果您使用循环中使用的实际url 打印出I(取消注释相应的行),您将看到:

    0 http://py4e-data.dr-chuck.net/known_by_Shannon.html
    ['Riya', 'Terri', 'Coban', 'Oswald', 'Codie', 'Arshjoyat', 'Carli', 'Aieecia', 'Ronnie', 'Yelena', 'Abid', 'Prithvi', 'Ellenor', 'Shayla', 'Chala', 'Nelson', 'Chaitanya', 'Stacey', 'Karis', 'Mariyah', 'Jamie', 'Breeanna', 'Kendall', 'Adelaide', 'Aimiee', 'Manwen', 'Dennys', 'Benjamyn', 'Reynelle', 'Jesuseun', 'Malik', 'Brigitte', 'Farah', 'Youcef', 'Ruqayah', 'Mili', 'Caitaidh', 'Raul', 'Katelyn', 'Yakup', 'Cohan', 'Lylakay', 'Dougray', 'Silvana', 'Roxanne', 'Tanchoma', 'Andie', 'Aarman', 'Kyalah', 'Tayyab', 'Malikah', 'Bo', 'Oona', 'Daniil', 'Wardah', 'Jessamy', 'Karly', 'Tala', 'Ilyaas', 'Maram', 'Ruaidhri', 'Donna', 'Liza', 'Aileigh', 'Muzzammil', 'Chi', 'Serafina', 'Abbas', 'Rhythm', 'Jonny', 'Niraj', 'Ciara', 'Kylen', 'Demmi', 'Christianna', 'Tanzina', 'Brianna', 'Kevyn', 'Hariot', 'Maisie', 'Naideen', 'Nicolas', 'Suvi', 'Areeb', 'Kiranpreet', 'Rachna', 'Umme', 'Caela', 'Miao', 'Tansy', 'Miah', 'Luciano', 'Karolina', 'Rivan', 'Cavan', 'Benn', 'Haydn', 'Zaina', 'Rafi', 'Ahmad']
    <a href="http://py4e-data.dr-chuck.net/known_by_Stacey.html">Stacey</a>
    1 http://py4e-data.dr-chuck.net/known_by_Ahmad.html
    ['Tilhi', 'Rachel', 'Latif', 'Deryn', 'Pawel', 'Anna', 'Blake', 'Brehme', 'Jo', 'Laurajane', 'Khayla', 'Declyan', 'Graidi', 'Foosiya', 'Nabeeha', 'Otilija', 'Dougal', 'Adeena', 'Alfie', 'Angali', 'Lilah', 'Saadah', 'Kelam', 'Kensey', 'Tabitha', 'Peregrine', 'Abdisalam', 'Presley', 'Allegria', 'Harish', 'Arshjoyat', 'Hussan', 'Sammy', 'Ama', 'Leydon', 'Anndra', 'Anselm', 'Logyne', 'Fion', 'Jacqui', 'Reggie', 'Mounia', 'Pedro', 'Hussain', 'Raina', 'Inka', 'Shaylee', 'Riya', 'Phebe', 'Uzayr', 'Isobella', 'Abdulkadir', 'Johndean', 'Charlotte', 'Moray', 'Saraah', 'Liana', 'Keane', 'Maros', 'Robi', 'Rowanna', 'Wesley', 'Maddox', 'Annica', 'Oluwabukunmi', 'Jiao', 'Nyomi', 'Hamish', 'Bushra', 'Marcia', 'Rimal', 'Kaceylee', 'Limo', 'Dela', 'Cal', 'Rhudi', 'Komal', 'Stevey', 'Amara', 'Nate', 'Roma', 'Fatou', 'Marykate', 'Abiya', 'Bay', 'Kati', 'Carter', 'Niraj', 'Maisum', 'Jaz', 'Coban', 'Harikrishna', 'Armani', 'Muir', 'Ilsa', 'Benjamyn', 'Russel', 'Emerson', 'Rehaan', 'Veronica']
    <a href="http://py4e-data.dr-chuck.net/known_by_Adeena.html">Adeena</a>
    

    为避免此问题,您必须将下一个循环的url 设置为第 18 个条目:

    import urllib.request, urllib.parse, urllib.error
    from bs4 import BeautifulSoup
    import ssl
    #import re
    term_counter = (0)
    file = list()
    #regex = list()
    # Ignore SSL certificate errors
    ctx = ssl.create_default_context()
    ctx.check_hostname = False
    ctx.verify_mode = ssl.CERT_NONE
    
    url = input('Enter - ')
    for _I in range(7) :
        #print(_I, url) <- this prints out the _I value of the loop and the url used in this round
        html = urllib.request.urlopen(url, context=ctx).read()
        soup = BeautifulSoup(html, 'html.parser')
        tags = soup('a')
        #print([item.contents[0] for item in tags]) <- this prints out a list of all names on this page
        file = list()
        for tag in tags :
            file.append(tag)
            #this is the last url you used in your code for the next _I loop
            url = tag.get('href') 
        #so we have to redefine url as the 18th entry in your list for the next _I loop round
        url = file[17].get("href")
        print("The next url we will use is {}".format(url))
    

    【讨论】:

    • 抱歉,我是 StackOverflow 的新手,我以为您在回答我的答案,因为您还编辑了问题。
    【解决方案3】:

    在我看来,班级的问题需要一种递归方法:

    import bs4
    import requests
    
    url = 'http://py4e-data.dr-chuck.net/known_by_Shannon.html'
    
    def get_url(url):
         page_response = requests.get(url)
         soup = BeautifulSoup(page_response.content, "html.parser")
         return soup.find_all('a')[17].get('href')
    
    def routine(url, counter):
        if counter > 0:
        url = get_url(url)
        print(url)
        counter -= 1
        routine(url, counter)
    
    routine(url, 7)
    

    输出:

    http://py4e-data.dr-chuck.net/known_by_Stacey.html
    http://py4e-data.dr-chuck.net/known_by_Zoya.html
    http://py4e-data.dr-chuck.net/known_by_Haiden.html
    http://py4e-data.dr-chuck.net/known_by_Ayrton.html
    http://py4e-data.dr-chuck.net/known_by_Leilan.html
    http://py4e-data.dr-chuck.net/known_by_Thorben.html
    http://py4e-data.dr-chuck.net/known_by_Jahy.html
    

    确认最后一个 url 名称的第一个字符以“J”开头。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 2010-11-16
      • 1970-01-01
      • 1970-01-01
      • 2012-09-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多