【问题标题】:Scraping author names from a website with try/except using Python使用 Python 使用 try/except 从网站抓取作者姓名
【发布时间】:2021-05-12 15:47:32
【问题描述】:

我正在尝试使用 Try/Except 来抓取包含作者数据的 URL 的不同页面。我需要一组来自本网站后续 10 个页面的作者姓名。

# Import Packages
import requests
import bs4
from bs4 import BeautifulSoup as bs
# Output list
authors = [] 
# Website Main Page URL
URL = 'http://quotes.toscrape.com/'
res = requests.get(URL)
soup = bs4.BeautifulSoup(res.text,"lxml")
# Get the contents from the first page
for item in soup.select(".author"):
    authors.append(item.text)
page = 1
pagesearch = True
# Get the contents from 2-10 pages
while pagesearch:
    # Check if page is available
    try:
            req = requests.get(URL + '/' + 'page/' + str(page) + '/')
            soup = bs(req.text, 'html.parser')
            page = page + 1
            for item in soup.select(".author"): # Append the author class from the webpage html
                authors.append(item.text)  
    except:
        print("Page not found")
        pagesearch == False
        break # Break if no page is remaining

print(set(authors)) # Print the output as a unique set of author names

第一页的 URL 中没有任何页码,因此我将其单独处理。我正在使用 try/except 块来遍历所有可能的页面,并在扫描最后一页时抛出异常并中断循环。

当我运行程序时,它进入了一个无限循环,当页面结束时它需要打印“页面未找到”消息。当我中断内核时,我看到正确的结果是一个列表和我的异常语句,但在那之前什么都没有。我得到以下结果。

Page not found
{'Allen Saunders', 'J.K. Rowling', 'Pablo Neruda', 'J.R.R. Tolkien', 'Harper Lee', 'J.M. Barrie', 
 'Thomas A. Edison', 'J.D. Salinger', 'Jorge Luis Borges', 'Haruki Murakami', 'Dr. Seuss', 'George 
  Carlin', 'Alexandre Dumas fils', 'Terry Pratchett', 'C.S. Lewis', 'Ralph Waldo Emerson', 'Jim 
  Henson', 'Suzanne Collins', 'Jane Austen', 'E.E. Cummings', 'Jimi Hendrix', 'Khaled Hosseini', 
 'George Eliot', 'Eleanor Roosevelt', 'André Gide', 'Stephenie Meyer', 'Ayn Rand', 'Friedrich 
  Nietzsche', 'Mother Teresa', 'James Baldwin', 'W.C. Fields', "Madeleine L'Engle", 'William 
  Nicholson', 'George R.R. Martin', 'Marilyn Monroe', 'Albert Einstein', 'George Bernard Shaw', 
 'Ernest Hemingway', 'Steve Martin', 'Martin Luther King Jr.', 'Helen Keller', 'Charles M. Schulz', 
 'Charles Bukowski', 'Alfred Tennyson', 'John Lennon', 'Garrison Keillor', 'Bob Marley', 'Mark 
  Twain', 'Elie Wiesel', 'Douglas Adams'}

这可能是什么原因?谢谢。

【问题讨论】:

    标签: python python-3.x web-scraping beautifulsoup try-except


    【解决方案1】:

    我认为这是因为字面上有一个页面。当浏览器上没有要显示的页面时,可能会出现异常。 但是当你提出这个请求时:

    http://quotes.toscrape.com/page/11/
    

    然后,浏览器显示一个页面,bs4 仍然可以解析得到一个元素。

    如何在第 11 页停止?您可以跟踪下一页按钮的存在。

    感谢阅读。

    【讨论】:

    • 是的,这可能是一个很好的方法。感谢您的回答。
    【解决方案2】:

    尝试使用内置的range() 函数从第 1-10 页改为:

    import requests
    from bs4 import BeautifulSoup
    
    url = "http://quotes.toscrape.com/page/{}/"
    authors = []
    
    for page in range(1, 11):
        response = requests.get(url.format(page))
        print("Requesting Page: {}".format(response.url))
        soup = BeautifulSoup(response.content, "html.parser")
        for tag in soup.select(".author"):
            authors.append(tag.text)
    
    print(set(authors))
    

    【讨论】:

    • 是的,这种方法可行,但是我正在寻找一种解决方案,它也适合网站中可能发生的变化。谢谢你的回答。
    猜你喜欢
    • 2021-09-06
    • 2020-09-28
    • 1970-01-01
    • 2016-05-27
    • 1970-01-01
    • 2017-06-04
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多