我正在编写一个代码来将值(一个接一个(发送到send
函数,该函数会将其作为 msg 发送给我。我有两个正则表达式keywords
allowed
和denied
列表,这些列表在re.search
中是允许的,哪些是被拒绝的。我正在从网络上抓取一个元素,试图用有效的re.search allowed
过滤它,并将其发送到我尝试第二次过滤它的second function
。这次过滤掉包含denied
单词的字符串。这就是问题所在。当我做一次循环时allowed
它很好,但是当我尝试为循环denied
做第二次循环时,它会将字符串发送到我的send
函数两次。我该如何更改代码以使其工作?
这是代码
allowed = ["pc", "FUJITSU", "LIFEBOOK", "win" "Windows",
"PC", "Linux" "linux", "HP", "hp", "notebook", "desktop",
"raspberry", "NEC", "mac", "Mac", "Core"]
denied = ["philips", "samsung"]
used = set()
source = requests.get("https://jmty.jp/aichi/sale-pcp").text
soup = BeautifulSoup(source, 'lxml')
def skype_login(user, password):
sk = Skype(user, password)
return(sk)
def send(sk, title, address, price, town, topic='Not'):
for c in sk.chats.recent():
chat = sk.chats[c]
if hasattr(chat, 'topic') and chat.topic == topic:
chat.sendMsg(f'Some string {title} n {price} n n {town} n n URL: {address}' )
break
sleep(1)
chat.sendMsg("Additional Message")
def jimoti(sk):
global used
for h2 in soup.find_all('div', class_='p-item-content-info'):
title = h2.select_one('.p-item-title').text
address = h2.select_one('.p-item-title').a["href"]
price = (h2.select_one('.p-item-most-important').text).replace("円", "").replace("n", "").replace(",", "")
price = int(price)
town = h2.select_one('.p-item-supplementary-info').text
if price < 2000 and title not in used:
used.add(title)
for pattern in allowed:
print(pattern)
if re.search(pattern, title):
second(sk, title, address, price, town)
break
def second(sk, title, address, price, town):
sk = sk
title = title
address = address
price = price
town = town
# for prh in denied: # Here it makes the problem
# print(prh)
# if re.search(prh, title):
# break
# else:
send(sk, title, address, price, town)
if __name__ == '__main__':
sk = skype_login('username', 'pass')
while True:
jimoti(sk)
sleep(randint(11,20))
如果我正确阅读了您的代码,您已经进行了此设置(伪 python(:
for bad in denied:
if bad exists in string:
break
else:
send message
这意味着对于每个未找到的拒绝单词,您都会发送消息。因此,如果有两个不好的词并且它不包含任何一个,那么您将发送两次。
您只需使用布尔值即可轻松解决此问题
def second(sk, title, address, price, town):
# I'm not sure why you do this, it's 100% unnecessary
# sk = sk
# title = title
# address = address
# price = price
# town = town
is_ok = True
for prh in denied:
if re.search(prh, title):
is_ok = False
break
if is_ok:
send(sk, title, address, price, town)