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