web-scraping - Beautiful Soup,抓取拍卖网站,在拍卖完成后清除已售物品 div
问题描述
我正在建造的刮刀遇到问题。我想从拍卖网站上抓取信息。问题是,当我请求 html 时,我必须刷新页面,当您刷新此拍卖网站页面时,任何已结束的拍卖物品都将被删除,因此我丢失了我想要获取的数据。
def soldDetection(soup):
#Select the timer and check if it == "Lot Closed" once lot is closed send the entire container information to getData() to extract price and details
while True:
getPage(url)
container = soup.find_all('li', class_='current-price')
#print(container)
for child in container:
label = child.span.contents[0]
#print (label)
if label == 'Closing bid':
# grab entire div for the card with data for the getData()
print('Found')
parentDiv = label.find_parent('div', class_='lot-single')
getData(parentDiv)
return parentDiv
else:
continue
time.sleep(1)
print('Nothing Sold')
continue
在我的浏览器中,如果我不刷新“当前价格”范围,内容会从“当前出价”更改为“收盘价”,但如果我刷新该项目,则会从 html 中清除。有没有办法在不提神醒脑的情况下得到漂亮的汤来观看这个?我担心 bs4 可能不是这项工作的正确工具,如果是,我应该使用什么工具?
谢谢,
解决方案
好吧,我找到了一个解决方案,我使用 selenium 打开浏览器并观察 javascript 发送到页面的更改。然后收集所有的 html 并将其发送到我美丽的汤函数以导航树。
from selenium import webdriver
browser = webdriver.Firefox()
browser.get(websiteAddress)
...
elems = browser.find_elements_by_xpath("//*[contains(text(),'Closing bid')]")
if not elems:
print('Not Found')
continue
label = elems[0].text
if label == 'Closing bid':
# grab entire div for the card with data for the getData()
soup = bs4.BeautifulSoup(browser.page_source,'html.parser')
推荐阅读
- c# - 为什么 HttpRequestMessage.Content.Headers.ContentType 会抛出空引用异常?
- android - 在项目中,它没有在单独的模块中看到库导入
- angular - Angular 8:TypeError:无法读取未定义的属性“种类”,当为生产构建我的代码时
- python - 使用正则表达式删除单词
- mongodb - 无法索引并行数组
- ms-word - 为了支持 createDocument OfficeJs API 调用需要实现什么 WOPI API
- reactjs - Fabric UI Modal 自定义尺寸
- scala - 从列表中更新 DataFrame col 名称,避免使用 var
- php - 用户输入 Localhost 时打开 Direct Project 或 Directory
- elasticsearch - 如何通过 Kibana 从 elasticsearch 中的文档更新值