使用 BeautifulSoup 在 python 中抓取多个页面

基兰

我已经设法编写代码来从第一页抓取数据,现在我坚持在此代码中编写一个循环来抓取下一个“n”页。下面是代码

如果有人可以指导/帮助我编写从剩余页面中抓取数据的代码,我将不胜感激。

谢谢!

from bs4 import BeautifulSoup
import requests
import csv


url = requests.get('https://wsc.nmbe.ch/search?sFamily=Salticidae&fMt=begin&sGenus=&gMt=begin&sSpecies=&sMt=begin&multiPurpose=slsid&sMulti=&mMt=contain&searchSpec=s').text

soup = BeautifulSoup(url, 'lxml')

elements = soup.find_all('div', style="border-bottom: 1px solid #C0C0C0; padding: 10px 0;")
#print(elements)

csv_file = open('wsc_scrape.csv', 'w')

csv_writer = csv.writer(csv_file)

csv_writer.writerow(['sp_name', 'species_author', 'status', 'family'])


for element in elements:
    sp_name = element.i.text.strip()
    print(sp_name)



    status = element.find('span', class_ = ['success label', 'error label']).text.strip()
    print(status)




    author_family = element.i.next_sibling.strip().split('|')
    species_author = author_family[0].strip()
    family = author_family[1].strip()
    print(species_author)
    print(family)


    print()

    csv_writer.writerow([sp_name, species_author, status, family])

csv_file.close()
奥尔德文

您必须page=在 URL 中传递参数并遍历所有页面:

from bs4 import BeautifulSoup
import requests
import csv

csv_file = open('wsc_scrape.csv', 'w', encoding='utf-8')
csv_writer = csv.writer(csv_file)
csv_writer.writerow(['sp_name', 'species_author', 'status', 'family'])

for i in range(151):
    url = requests.get('https://wsc.nmbe.ch/search?page={}&sFamily=Salticidae&fMt=begin&sGenus=&gMt=begin&sSpecies=&sMt=begin&multiPurpose=slsid&sMulti=&mMt=contain&searchSpec=s'.format(i+1)).text
    soup = BeautifulSoup(url, 'lxml')
    elements = soup.find_all('div', style="border-bottom: 1px solid #C0C0C0; padding: 10px 0;")
    for element in elements:
        sp_name = element.i.text.strip()
        print(sp_name)
        status = element.find('span', class_ = ['success label', 'error label']).text.strip()
        print(status)
        author_family = element.i.next_sibling.strip().split('|')
        species_author = author_family[0].strip()
        family = author_family[1].strip()
        print(species_author)
        print(family)
        print()
        csv_writer.writerow([sp_name, species_author, status, family])

csv_file.close()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用BeautifulSoup和Python刮取多个页面

如何使用python和beautifulsoup4循环抓取网站中多个页面的数据

无法使用python和beautifulsoup抓取网页中的某些href

如何使用BeautifulSoup和Python抓取页面?

如何在一个Web上从多个页面抓取数据,我正在使用Python和BeautifulSoup

在Python 3中使用BeautifulSoup抓取网址

在Python中浏览Selenium并使用BeautifulSoup进行抓取

如何使用BeautifulSoup从页面抓取

Web使用BeautifulSoup抓取多个页面

如何使用Python和BeautifulSoup抓取多个Google页面

使用python中的BeautifulSoup从网站抓取报告

如何使用BeautifulSoup创建循环以从源URL抓取多个页面?

使用BeautifulSoup按Python中的元素抓取HTML

使用BeautifulSoup Python抓取网页

Python-使用BeautifulSoup在页面内抓取多个类

python beautifulsoup抓取存档页面

使用python为多个页面抓取网页

使用 BeautifulSoup 和 Python 抓取多个表格页面

如何使用python BeautifulSoup通过分页抓取页面

网页抓取 - 从使用 BeautifulSoup 和 Python 的类中获取文本?

使用嵌套 for 循环抓取网页,python3 中的 BeautifulSoup

使用beautifulsoup python从页面中抓取特定元素时遇到问题

使用 Python 3.7 中的 Beautifulsoup 从 WSJ 抓取网页文章?

如何使用beautifulsoup从python中的url中抓取数据

使用 BeautifulSoup 抓取:从 HTML 页面抓取表格中的特定列

使用 python 抓取网站 - BeautifulSoup

使用 BeautifulSoup 从多个页面下载多线程文件的网页抓取

使用beautifulsoup在Python中迭代页面

使用 BeautifulSoup 在 Steam 中抓取多个页面