python - 在 Scrapy 中使用字体或颜色抓取网站
问题描述
我需要从网站上刮下价格,我遇到了一个问题,其中某些价格被划掉,新价格以红色/粗体字母显示,并且该代码的 html 代码不同,所以我的价格为空。所以我决定做一个 if 语句来获取正确的数据,但唯一的问题是,划掉的价格具有相同的标识符,所以我得到的是那个价格而不是红色的那个。那么Scrapy有没有办法根据颜色为红色或字体为粗体来获取我需要的价格?如果没有,我还有其他方法可以得到合适的价格吗?
for game in response.css("tr[class^=deckdbbody]"):
# Initialize saved_name to the extracted card name
saved_name = game.css("a.card_popup::text").extract_first() or saved_name
# Now call item and set equal to saved_name and strip leading '\n' from output
item["Card_Name"] = saved_name.strip()
# Check to see if output is null, in the case that there are two different conditions for one card
if item["Card_Name"] != None:
# If not null than store value in saved_name
saved_name = item["Card_Name"].strip()
# If null then set null value to previous card name since if there is a null value you should have the same card name twice
else:
item["Card_Name"] = saved_name
# Call item again in order to extract the condition, stock, and price using the corresponding html code from the website
item["Condition"] = game.css("td[class^=deckdbbody].search_results_7 a::text").get()
item["Stock"] = game.css("td[class^=deckdbbody].search_results_8::text").extract_first()
item["Price"] = game.css("td[class^=deckdbbody].search_results_9::text").extract_first()
if item["Price"] == None:
item["Price"] = game.css("td[class^=deckdbbody].search_results_9 span::text").get()
# Return values
yield item
解决方案
您可以使用样式属性对其进行过滤
response.css('span[style^="color:red;"]::text').get()
推荐阅读
- node.js - 使用 Node.js 和 Flutter 进行用户身份验证
- c++ - 静态内联函数中的函数局部静态对象不共享
- ios - 从firebase加载位置到MapView的Swift 5错误
- javascript - Javascript(html)Math.random()范围不起作用
- reactjs - react-bootstrap-table2 搜索不适用于格式化单元格
- python-3.x - 在 Pandas DataFrame 中加载 csv 文件时将字符串转换为设置数组
- python - 如何从另一个函数访问变量
- lua - 我想对其进行编码,以便当我在 roblox 中打开聊天并按下单词中包含字母“r”的内容时,我不会重置
- proto - 如何使用不同的文件导入正确运行 protoc?
- node.js - 使用 ELB 在 AWS EC2 上的套接字 IO net::ERR_CONNECTION_REFUSED