首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用try/除了使用Python从网站中抓取作者姓名

使用try/除了使用Python从网站中抓取作者姓名
EN

Stack Overflow用户
提问于 2021-05-12 15:47:32
回答 2查看 115关注 0票数 0

我试图使用Try/除非是为了浏览包含作者数据的URL的不同页面。我需要一套作者的名字,从10个后续的网页本网站。

代码语言:javascript
复制
# 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/ for块来迭代所有可能的页面,并在扫描最后一个页面时抛出一个异常并中断循环。

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

代码语言:javascript
复制
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'}

这是什么原因?谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-05-12 16:07:53

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

代码语言:javascript
复制
http://quotes.toscrape.com/page/11/

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

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

感谢您的阅读。

票数 0
EN

Stack Overflow用户

发布于 2021-05-12 17:20:21

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

代码语言:javascript
复制
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))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67506934

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档