[英]Scraping Dynamic Data and Avoiding Duplicates with BS4, Selenium in Python
我在這里要做的是從動態頁面檢索數據,不斷重新加載信息。 我設置的方式是每60秒刷新一次。 問題是,舊數據不會從頁面中刪除,因此當程序在刷新后通過數據時,會有重復數據。
注意:程序在開始時就會休眠,因為最初沒有要刪除的消息。
我正在尋找一種方法或解決方案來使用最后一條記錄(在這種情況下將是消息[-1])作為搜索的起點,以防止重復。
感謝所有幫助! 謝謝。
driver.get(URL)
while 1==1:
time.sleep(60)
chat_page = driver.page_source
chat_soup = BeautifulSoup(chat_page,'lxml')
messages = chat_soup.findAll('div', attrs={'class':'message first'})
for message in messages:
username = message.div.h2.span.strong.text
text = message.find('div', attrs={'class':'markup'}).get_text()
timestamp = message.find('span', attrs={'class':'timestamp'}).get_text()
today = str(datetime.date.today())
timestamp = timestamp.replace('Today', today)
usernames.append(username)
timestamps.append(timestamp)
texts.append(text)
print(timestamp, username," : ",text)
我創建了一個臨時解決方案,在將每個記錄輸入我的SQLite3數據庫之前檢查每個記錄。 該程序能夠使用“INSERT OR IGNORE”工作。 不幸的是,該程序不斷檢查重復項,因為它無法過濾掉已經被刪除的數據。 以下是我的臨時解決方案:
driver.get(URL)
while 1==1:
chat_page = driver.page_source
chat_soup = BeautifulSoup(chat_page,'lxml')
messages = chat_soup.findAll('div', attrs={'class':'message first'})
for message in reversed(messages):
username = message.div.h2.span.strong.text
usernames.append(username)
text = message.find('div', attrs={'class':'markup'}).get_text()
text = text.replace('"', '')
text = text.replace("'", "")
username = username.replace('"', '')
username = username.replace("'", "")
timestamp = message.find('span', attrs={'class':'timestamp'}).get_text()
today = str(datetime.date.today())
timestamp = timestamp.replace('Today', today)
isbot = message.find('span', attrs={'class':'bot-tag'})
if (isbot):
username = '(BOT) ' + username
sql = '''INSERT OR IGNORE INTO 'chats' ('timestamp', 'username', 'text') VALUES ("%s", "%s", "%s")''' % (timestamp, username, text)
conn.executescript(sql)
我找到了一個使用set.difference的解決方案,效果很好。
在我的問題中,一次存在一定數量的數據(比方說10)。 我們希望在沒有舊的情況下獲得新的價值。
olddata = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
newdata = [5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
unique_data = set(newdata).difference(olddata)
=
{11, 12, 13, 14}
最終工作守則:
while 1==1:
chat_page = driver.page_source
chat_soup = BeautifulSoup(chat_page,'lxml')
messages = chat_soup.findAll('div', attrs={'class':'message first'})
messages_dedupe = set(messages).difference(oldlist)
for message in messages_dedupe:
username = message.div.h2.span.strong.text
text = message.find('div', attrs={'class':'markup'}).get_text()
timestamp = message.find('span', attrs={'class':'timestamp'}).get_text()
today = str(datetime.date.today())
timestamp = timestamp.replace('Today', today)
isbot = message.find('span', attrs={'class':'bot-tag'})
if (isbot):
username = '(BOT) ' + username
usernames.append(username)
timestamps.append(timestamp)
texts.append(text)
oldlist = messages
sqlvalues = (username, timestamp, text)
c.execute("INSERT OR IGNORE INTO db (username, timestamp, text) VALUES (?, ?, ?)", sqlvalues)
conn.commit()
print(timestamp, username,":",text)
time.sleep(20)
那么你正在尋找一種方法來避免檢查每個記錄的重復? 假設每個時間戳都是唯一值,並且從最新消息到最舊消息的順序reversed(messages)
。
timestamp_array = []
while 1==1:
chat_page = driver.page_source
chat_soup = BeautifulSoup(chat_page,'lxml')
messages = chat_soup.findAll('div', attrs={'class':'message first'})
for message in reversed(messages):
username = message.div.h2.span.strong.text
usernames.append(username)
text = message.find('div', attrs={'class':'markup'}).get_text()
text = text.replace('"', '')
text = text.replace("'", "")
username = username.replace('"', '')
username = username.replace("'", "")
timestamp = message.find('span', attrs={'class':'timestamp'}).get_text()
today = str(datetime.date.today())
timestamp = timestamp.replace('Today', today)
isbot = message.find('span', attrs={'class':'bot-tag'})
if (isbot):
username = '(BOT) ' + username
if timestamp in timestamp_array:
break
timestamp_array.append(timestamp)
sql = '''INSERT OR IGNORE INTO 'chats' ('timestamp', 'username', 'text') VALUES ("%s", "%s", "%s")''' % (timestamp, username, text)
conn.executescript(sql)
一旦達到第一個副本,這將突破for循環。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.