【发布时间】:2022-02-03 11:53:30
【问题描述】:
我正在尝试从https://www.fairprice.com.sg/product/magnolia-fresh-milk-1lt-13022014 删除促销价格详细信息
具体来说,我正在尝试取消“Any 2 for $5.45,Save $1.55”的信息。当我运行下面的代码时,它给了我一个空返回。
在同一网站的其他产品上使用相同的代码虽然有效(例如https://www.fairprice.com.sg/product/kirei-kirei-hand-soap-rfl-moisturing-peach-200ml-12089153)
不确定导致行为差异的原因。感谢您对此问题的任何建议。
import sys
import time
from bs4 import BeautifulSoup
import requests
import re
try:
url = 'https://www.fairprice.com.sg/product/magnolia-fresh-milk-1lt-13022014'
headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36 Edg/97.0.1072.69'}
page=requests.get(url, headers=headers)
except Exception as e:
error_type, error_obj, error_info = sys.exc_info()
print ('ERROR FOR LINK:', url)
print (error_type, 'Line:', error_info.tb_lineno)
time.sleep(2)
soup=BeautifulSoup(page.text,'html.parser')
linkpromo=soup.find_all('span',attrs={'class':'sc-1bsd7ul-1 eSToaS'},string=re.compile(r'Any'))
print(linkpromo)
【问题讨论】:
-
您要查找的内容实际上并未加载到页面上。如果您在尝试查找所需信息之前打印您的汤对象,您将不会看到它作为跨度的一部分,就像您在浏览器上看到的那样。但是您可以在页面上的最终脚本标记中看到该信息
标签: python html web-scraping beautifulsoup python-requests