【问题标题】:How to do web scraping in python running raspberry pi lite os?如何在运行 raspberry pi lite os 的 python 中进行网页抓取?
【发布时间】:2022-01-25 18:37:33
【问题描述】:

需要帮助来使用在 raspberry pi lite 64 位操作系统中运行的 python 来跟踪网站上的产品价格。我尝试了两种方法都没有成功。

以下是用于我的网络抓取的 Python、beautiful soup 和 scrapy 的 OS 和版本详细信息

$ uname -m
  aarch64

$ python --version
  Python 3.7.3

$ scrapy version
  Scrapy 2.5.1

>>> bs4.__version__
'4.10.0'

方法一:requests_html

webscrapping.py

from bs4 import BeautifulSoup
from requests_html import HTMLSession


emailcontent = "Subject: My daily digest \n"


def web_scrape_function(htmlid, emailcontent, classselector, webrequesturl):
    i = 1
    session = HTMLSession()
    r = session.get(webrequesturl)
    r.html
    soup = BeautifulSoup(r.text, 'html.parser')
    result = soup.find_all(htmlid, attrs={'class': classselector})
    r.session.close()
    for eachresult in result:
        emailcontent += ("%s- " % i)
        emailcontent += eachresult.text.strip()
        emailcontent += "\n"
        i += 1

    print(emailcontent)
    session.close()


# Endy pillow


web_scrape_function("span", emailcontent, "original-price",
                    "https://ca.endy.com/products/the-customizable-pillow?gclid=CjwKCAiAn5uOBhADEiwA_pZwcDx8pC80pUaaiLTsHof33OPqrUOEDZ3v-ogIcdJ5PdSrvcR-WxVGSxoCWkUQAvD_BwE&size=king")

输出

$ python3 webscrapping.py 

Subject: My daily digest 
1- ${{this.numberWithCommas(this.formatPrice(this.selected_variant.price))}}
    $
2- ${{this.numberWithCommas(this.formatPrice(this.selected_variant.price))}}
    $
3- Out of Stock

${{formatPrice(size.price)}} 

方法 2:Scrapy

myspider.py

import scrapy


class QuotesSpider(scrapy.Spider):
    name = "peppe8o"
    start_urls = [
        'https://ca.endy.com/products/the-customizable-pillow?gclid=CjwKCAiAn5uOBhADEiwA_pZwcDx8pC80pUaaiLTsHof33OPqrUOEDZ3v-ogIcdJ5PdSrvcR-WxVGSxoCWkUQAvD_BwE&size=king',
    ]

    def parse(self, response):
        for prices in response.css('div.flex-container div.desktop-product-price'):
            yield {
                'price': prices.css('div.flex-container span.original-price::text').get(),
            }

输出

$ scrapy runspider myspider.py -o peppe8o.json

$ cat peppe8o.json
  [
   {"price": "\n    "}
  ]

在上述两种方法中,我都无法获得 95 美元的价值。上述两种方法似乎适用于某些静态网站,这让我相信我只遇到使用 javascript 渲染的动态网页的问题。请提出更正或替代代码,以实现我正在寻找的内容。

【问题讨论】:

    标签: python web-scraping raspberry-pi


    【解决方案1】:

    没错。 requestsscrapy 都没有 Javascript 解释器。您所得到的只是交付的静态 HTML。如果您需要解释 Javascript,那么您必须使用真正的浏览器,这通常意味着 Selenium。

    【讨论】:

    • 谢谢。我将发布一个关于如何在 aarch64 中安装和运行 selenium headless 的单独问题。我只看到 x86、x64、amd64、arm64 的指南。
    猜你喜欢
    • 2020-08-25
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 2016-12-16
    • 1970-01-01
    • 2011-10-21
    • 1970-01-01
    相关资源
    最近更新 更多