【发布时间】:2020-08-03 15:37:02
【问题描述】:
我正在 python 3.8 中编写一个带有 solenium 的网络自动化机器人,它从管理界面中提取销售数据,通过管理界面发送消息以感谢他们的购买。销售额保存在具有以下结构的表中。
<tr id="sale_row">
<td>Month | Day | Year | time of sale</td>
<td>Customer name | <a href="Link to message customer">Message Customer</a></td>
<td>item Purchased</td>
<td>Purchase Amount</td>
</tr>
我只想将消息发送到最近 x 天的订单(这是导致问题的原因,因为我似乎无法将日期链接到 href)。
到目前为止,我已经设法只选择了我想要检索的日期,但无法弄清楚如何将包含 href 的第二个 td 获取到消息传递系统。
为了匹配日期,我创建了一个与时间戳格式匹配的字符串,然后在所需的天数范围内运行 for 循环并将输出附加到列表中,另一个循环遍历页面以提取日期然后将它们与想要的日期列表进行比较并检查是否匹配。 (日期保存在第一个 td 中,这是唯一一个具有“text-left”类的日期
cm = today_date.strftime("%b")
cd = today_date.day
target_day = cd - 7
days = range(target_day,cd+1)
sold_month = bot.find_elements_by_class_name("text-left")
target_days = []
active_sales =[]
for day in days:
target_date = target_days.append(cm+' '+str(day)+','+' '+str(cy))
for month in sold_month:
sale=active_sales.append(month.text[:-10])
for active_sale in active_sales:
#print("Active sale:",active_sale)
for target_day in target_days:
# print("Target Day:",target_days)
if target_day == active_sale:
print("Result:", active_sale)
以上提供只选择没有问题的日子。为了在第二个 td 上获取消息系统的 href,我在最后一个 if 语句中尝试了以下操作,它返回每次销售的所有链接,这是有道理的,但我似乎无法弄清楚如何获取消息系统的 href基于第一个 td 在日期范围内的第二个 td:
sales = bot.find_elements_by_xpath('//a[contains(@href, "/Inbox/New/")]')
所有消息链接都不同,因为它们包含客户 ID,这就是为什么它们需要与日期匹配。
【问题讨论】:
标签: python-3.x matching webautomation