【问题标题】:Python scraping go to next page using BeautifulSoup [closed]Python抓取使用BeautifulSoup进入下一页[关闭]
【发布时间】:2019-03-14 02:52:56
【问题描述】:

这是我的抓取代码:

import requests
from bs4 import BeautifulSoup as soup
def get_emails(_links:list):
for i in range(len(_links)):
 new_d = soup(requests.get(_links[i]).text, 'html.parser').find_all('a', {'class':'my_modal_open'})
 if new_d:
   yield new_d[-1]['title']

start=20
while True:
d = soup(requests.get('http://www.schulliste.eu/type/gymnasien/?bundesland=&start=20').text, 'html.parser')

results = [i['href'] for i in d.find_all('a')][52:-9]
results = [link for link in results if link.startswith('http://')]
print(list(get_emails(results)))

next_page=soup.find('div', {'class': 'paging'}, 'weiter')

if next_page:

    d=next_page.get('href')
    start+=20
else:
    break

这就是我得到的错误: AttributeError: 'str' 对象没有属性 'find_all'

当您按下按钮“weiter”(下一页)时,urlending 从“...start=20”变为“start=40”。 因为每个站点有 20 个结果,所以它在 20 秒内。 有谁知道错误的原因吗?

【问题讨论】:

  • 这里的代码没有给soup分配一个字符串,它绑定到从顶部bs4导入的BeautifulSoup。您可能在其他地方为它分配了一些东西,但不是在此处的代码中。您本地的 BeautifulSoup instance 称为 d

标签: python web-scraping beautifulsoup next attributeerror


【解决方案1】:

您将“汤”放入名为“d”的变量中。

所以替换以下行:

next_page=soup.find('div', {'class': 'paging'}, 'weiter')

有了这个:

next_page = d.find('div', {'class': 'paging'}, 'weiter')

【讨论】:

  • 是的,谢谢。但是 python 并没有走到下一个方面,它只是一次又一次地打印出相同的电子邮件地址。你现在为什么?还是我应该提出一个新问题?
  • 请看下面的回答:stackoverflow.com/a/43076493/7547749
猜你喜欢
  • 2022-06-13
  • 1970-01-01
  • 1970-01-01
  • 2020-10-04
  • 2021-01-31
  • 1970-01-01
  • 2021-02-12
  • 1970-01-01
  • 2016-07-01
相关资源
最近更新 更多