【问题标题】:How can I scrape multiple pages using Beautiful Soup?如何使用 Beautiful Soup 抓取多个页面?
【发布时间】:2018-11-25 00:35:56
【问题描述】:

如何从网站上抓取多个页面?此代码仅适用于第一个:

import csv
import requests
from bs4 import BeautifulSoup

import datetime

filename = "azet_" + datetime.datetime.now().strftime("%Y-%m-%d-%H-%M")+".csv"
with open(filename, "w+") as f:
    writer = csv.writer(f)
    writer.writerow(["Descriere","Pret","Data"])
    
    r = requests.get("https://azetshop.ro/12-extensa?page=1")

    soup = BeautifulSoup(r.text, "html.parser")
    x = soup.find_all("div", "thumbnail")

    for thumbnail in x:
        descriere = thumbnail.find("h3").text.strip()
        pret = thumbnail.find("price").text.strip()
       
        writer.writerow([descriere, pret, datetime.datetime.now()]) 

【问题讨论】:

  • 在不了解网站布局的情况下很难回答。也许第一张图像和其他图像之间存在差异?您是否检查过“x”变量中有多少项目?也许 soup.find_all(...) 只找到一个?
  • hello cris.sp 你测试你的代码了吗?输出填充为空?

标签: python web-scraping beautifulsoup


【解决方案1】:

对于使用BeautifulSoup 进行多页抓取,许多人通常使用while

import csv
import requests
from bs4 import BeautifulSoup    
import datetime

end_page_num = 50

filename = "azet_" + datetime.datetime.now().strftime("%Y-%m-%d-%H-%M")+".csv"
with open(filename, "w+") as f:
    
    writer = csv.writer(f)
    writer.writerow(["Descriere","Pret","Data"])
    i = 1
    while i <= end_page_num:
        
        r = requests.get("https://azetshop.ro/12-extensa?page={}".format(i))

        soup = BeautifulSoup(r.text, "html5lib")
        x = soup.find_all("div", {'class': 'thumbnail-container'})

        for thumbnail in x:
            descriere = thumbnail.find('h1', {"class": "h3 product-title"}).text.strip()
            pret = thumbnail.find('span', {"class": "price"}).text.strip()
            writer.writerow([descriere, pret, datetime.datetime.now()])
        i += 1

这里i 将随着页面抓取完成而随着1 的增加而变化。 这将继续抓取直到您定义的end_page_num

【讨论】:

  • 你好,你测试你的代码了吗?输出填充为空?
  • @AmaraBOUDIB 哦,谢谢,OP 说代码有效,就是这样的印象。
【解决方案2】:

此代码也可以很好地使用 bs4 的类属性:

            import csv
            import requests
            from bs4 import BeautifulSoup
            import datetime
            
            filename = "azet_" + datetime.datetime.now().strftime("%Y-%m-%d-%H-%M")+".csv"
            with open(filename, "w+") as f:
                writer = csv.writer(f)
                writer.writerow(["Descriere","Pret","Data"])
            
                for i in range(1,50):
                    r = requests.get("https://azetshop.ro/12-extensa?page="+format(i))
                
                    soup = BeautifulSoup(r.text, "html.parser")
                    array_price= soup.find_all('span', class_='price')
                    array_desc=soup.find_all('h1', class_='h3 product-title',text=True)
                    for iterator in range(0,len(array_price)):
                        descriere = array_desc[iterator].text.strip()
                        pret = array_price[iterator].text.strip()
                       
                        writer.writerow([descriere, pret, datetime.datetime.now()]) 
             

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-16
    • 2020-04-28
    • 2017-03-30
    • 2019-11-14
    • 1970-01-01
    • 1970-01-01
    • 2016-11-19
    • 2019-05-12
    相关资源
    最近更新 更多