【发布时间】: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