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

海伦娜
   # -*- coding: utf-8 -*-
"""
Created on Fri Jun 29 10:38:46 2018

@author: Cinthia
"""

from bs4 import BeautifulSoup as soup
from urllib.request import urlopen as uReq
array = ['146-face', '153-palettes-sets', 'https://www.sociolla.com/147-eyes', 'https://www.sociolla.com/150-lips', 'https://www.sociolla.com/149-brows', 'https://www.sociolla.com/148-lashes']
base_url='https://www.sociolla.com/142-face'
uClient = uReq(base_url)
page_html = uClient.read()
uClient.close()

#html parsing
page_soup = soup(page_html, "html.parser")

#grab the product
kosmetik = page_soup.findAll("div", {"class":"col-md-3 col-sm-6 ipad-grid col-xs-12 productitem"})
print(len(kosmetik))

我想从该网站上抓取数据,上面的代码仅在基本网址上占用了多少产品。我不知道该数组如何工作,因此它可以从产品中获取的数据(例如描述,图像,价格)从我在数组中创建的所有页面中获取。

我是Python的新手,对循环了解不多。

贝特朗·马特尔

您可以在id=product-list-grid此处找到表/网格的根元素,并提取包含所有需要的信息(品牌,链接,类别)和第一个<img>标签的属性

对于分页,似乎可以添加到下一页p=<page number>,而当该页面不存在时,它将重定向到第一页。一种解决方法是检查响应URL,并检查其是否与您请求的URL相同。如果相同,则可以增加页码,否则将所有页面刮掉

from bs4 import BeautifulSoup
import urllib.request

count = 1
url = "https://www.sociolla.com/142-nails?p=%d"

def get_url(url):
    req = urllib.request.Request(url)
    return urllib.request.urlopen(req)

expected_url = url % count
response = get_url(expected_url)

results = []

while (response.url == expected_url):
    print("GET {0}".format(expected_url))
    soup = BeautifulSoup(response.read(), "html.parser")

    products = soup.find("div", attrs = {"id" : "product-list-grid"})

    results.append([
        (
            t["data-eec-brand"],    #brand
            t["data-eec-category"], #category
            t["data-eec-href"],     #product link
            t["data-eec-name"],     #product name
            t["data-eec-price"],    #price
            t.find("img")["src"]    #image link
        ) 
        for t in products.find_all("div", attrs = {"class" : "product-item"})
        if t
    ])

    count += 1
    expected_url = url % count
    response = get_url(expected_url)

print(results)

结果存储在这里results,是一个元组数组

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

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

在python和beautifulsoup中查找同一个<div>内的多个抓取数据

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

我正在尝试删除使用Python和BeautifulSoup抓取的Web链接的重复数据,但是它不起作用

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

使用此代码,我可以从第一个网址获得作者和书名的列表!如何使用BeautifulSoup抓取多个URL数据?

如何抓取一个需要使用python和beautifulsoup登录的网站?

我正在尝试使用jquery和ajax将多个数据发送到另一个页面,以检查数据库中是否存在值

我正在尝试使用 Python 将抓取的数据保存到 CSV 文件,但得到一个 TypeError

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

Web使用BeautifulSoup抓取多个页面

使用 BeautifulSoup 和 Selenium 抓取一个网站的多个网页的内容

如何从python和beautifulsoup中的页面抓取iframe数据范围

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

我应该如何使用BeautifulSoup将多个页面的表附加到一个CSV文件中?

我们如何在一个php页面中调用多个php页面?

如何使用 Python 和 BeautifulSoup 从 html 表中抓取数据?

如何使用 Python 和 BeautifulSoup 抓取数据移动到下一页

使用BeautifulSoup的Python Web抓取,如何将两个<p>文本合并到列表的一个元素中

我如何在 ScrollView @shoutem/ui 中使用 web 视图和一个视图

如何在 selenium python BeautifulSoup 上循环多个页面

我的函数在调用时只返回列表的第一个元素。我正在使用 BeautifulSoup 提取数据

如何在 Python 中使用来自一个 Excel 列的多个数据点?

从页面中抓取一个项目在 python beautifulsoup 中返回 None

我正在尝试使用 beautfiul 汤抓取多个页面,但代码不断为每个页面返回相同的数据

我如何在一个页面中有多个导航栏

我应该如何在应用程序的多个页面上显示一个文本块?

如何在一个 FORM 上创建多个页面而不显示标签页

如何在具有多个关系的一个节点上聚合页面节点