我正在抓取帖子的网页并将帖子保存到csv文件中,并试图防止多次保存同一帖子。
到目前为止,我可以抓住并保存帖子,但是我试图将新帖子与我已经保存但失败的帖子进行比较。我的代码是:
import csv
import requests
from bs4 import BeautifulSoup
r = requests.get("https://tpwd.texas.gov/state-parks/ray-roberts-lake/park_events")
page = BeautifulSoup(r.text, "html.parser")
events = page.find("div", "eventlist_month")
tweeted =[]
read = []
tweet = []
tweets = []
with open("C:/Users/Shua/Desktop/output.csv", "r") as f:
reader = csv.reader(f)
for row in reader:
read = row
tweeted.append(read)
#print (read)
for tags in events.find_all("div", "vevent event"):
event_date = tags.find("abbr", "dtstart").text.strip()
event_title = tags.find("h3").text
event_local = tags.find("div", "location").text
event_sum = tags.find("div", "description").text
event_url = tags.find("a")["href"]
event_date2 = " ".join(event_date.split())
tweet = [event_date2+" "+event_title+"\n"+event_sum+"\n"+event_local+" "+event_url]
result = any(elem in tweet for elem in tweeted)
print (tweeted)
print(tweet)
print(result)
if result:
print("saved")
else:
print("not save")
tweets.append(tweet)
,输出为
[["Jun 28 Dutch Oven Cookin'\nJoin us for a tasty cooking lesson!\nRay Roberts Lake State Park - Isle Du Bois Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-isle-du-bois-unit/dutch-oven-breakfast"], ['Jun 28 Friday Night Frog Talk\nCome see our green, long-legged friends!\nRay Roberts Lake State Park - Johnson Branch Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-johnson-branch-unit/friday-night-frog-talk-8'], ['Jun 28 Wonders of the Night\nJoin a ranger for a night hike to find what goes bump in the night!\nRay Roberts Lake State Park - Isle Du Bois Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-isle-du-bois-unit/wonders-of-the-night-2'], ['Jun 29 Guided Nature Hike\nLearn about the local flora and fauna with a Texas Master Naturalist volunteer.\nRay Roberts Lake State Park - Isle Du Bois Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-isle-du-bois-unit/master-naturalist-guided-hike-5'], ['Jun 29 Hike with a Ranger!\nHike with a Ranger through Dogwood Canyon.\nRay Roberts Lake State Park - Johnson Branch Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-johnson-branch-unit/dogwood-canyon-hike-2'], ["Jun 29 Pond Dipping\nLet's explore the pond life!\nRay Roberts Lake State Park - Johnson Branch Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-johnson-branch-unit/pond-dipping"], ['Jun 29 Arts in the Park\nTry your hand at watercolors!\nRay Roberts Lake State Park - Johnson Branch Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-johnson-branch-unit/arts-in-the-park-1']]
["Jun 28 Dutch Oven Cookin'\nJoin us for a tasty cooking lesson!\nRay Roberts Lake State Park - Isle Du Bois Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-isle-du-bois-unit/dutch-oven-breakfast"]
False
not save
[["Jun 28 Dutch Oven Cookin'\nJoin us for a tasty cooking lesson!\nRay Roberts Lake State Park - Isle Du Bois Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-isle-du-bois-unit/dutch-oven-breakfast"], ['Jun 28 Friday Night Frog Talk\nCome see our green, long-legged friends!\nRay Roberts Lake State Park - Johnson Branch Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-johnson-branch-unit/friday-night-frog-talk-8'], ['Jun 28 Wonders of the Night\nJoin a ranger for a night hike to find what goes bump in the night!\nRay Roberts Lake State Park - Isle Du Bois Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-isle-du-bois-unit/wonders-of-the-night-2'], ['Jun 29 Guided Nature Hike\nLearn about the local flora and fauna with a Texas Master Naturalist volunteer.\nRay Roberts Lake State Park - Isle Du Bois Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-isle-du-bois-unit/master-naturalist-guided-hike-5'], ['Jun 29 Hike with a Ranger!\nHike with a Ranger through Dogwood Canyon.\nRay Roberts Lake State Park - Johnson Branch Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-johnson-branch-unit/dogwood-canyon-hike-2'], ["Jun 29 Pond Dipping\nLet's explore the pond life!\nRay Roberts Lake State Park - Johnson Branch Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-johnson-branch-unit/pond-dipping"], ['Jun 29 Arts in the Park\nTry your hand at watercolors!\nRay Roberts Lake State Park - Johnson Branch Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-johnson-branch-unit/arts-in-the-park-1']]
['Jun 28 Friday Night Frog Talk\nCome see our green, long-legged friends!\nRay Roberts Lake State Park - Johnson Branch Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-johnson-branch-unit/friday-night-frog-talk-8']
False
not save
[["Jun 28 Dutch Oven Cookin'\nJoin us for a tasty cooking lesson!\nRay Roberts Lake State Park - Isle Du Bois Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-isle-du-bois-unit/dutch-oven-breakfast"], ['Jun 28 Friday Night Frog Talk\nCome see our green, long-legged friends!\nRay Roberts Lake State Park - Johnson Branch Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-johnson-branch-unit/friday-night-frog-talk-8'], ['Jun 28 Wonders of the Night\nJoin a ranger for a night hike to find what goes bump in the night!\nRay Roberts Lake State Park - Isle Du Bois Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-isle-du-bois-unit/wonders-of-the-night-2'], ['Jun 29 Guided Nature Hike\nLearn about the local flora and fauna with a Texas Master Naturalist volunteer.\nRay Roberts Lake State Park - Isle Du Bois Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-isle-du-bois-unit/master-naturalist-guided-hike-5'], ['Jun 29 Hike with a Ranger!\nHike with a Ranger through Dogwood Canyon.\nRay Roberts Lake State Park - Johnson Branch Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-johnson-branch-unit/dogwood-canyon-hike-2'], ["Jun 29 Pond Dipping\nLet's explore the pond life!\nRay Roberts Lake State Park - Johnson Branch Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-johnson-branch-unit/pond-dipping"], ['Jun 29 Arts in the Park\nTry your hand at watercolors!\nRay Roberts Lake State Park - Johnson Branch Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-johnson-branch-unit/arts-in-the-park-1']]
['Jun 28 Wonders of the Night\nJoin a ranger for a night hike to find what goes bump in the night!\nRay Roberts Lake State Park - Isle Du Bois Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-isle-du-bois-unit/wonders-of-the-night-2']
False
not save
[["Jun 28 Dutch Oven Cookin'\nJoin us for a tasty cooking lesson!\nRay Roberts Lake State Park - Isle Du Bois Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-isle-du-bois-unit/dutch-oven-breakfast"], ['Jun 28 Friday Night Frog Talk\nCome see our green, long-legged friends!\nRay Roberts Lake State Park - Johnson Branch Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-johnson-branch-unit/friday-night-frog-talk-8'], ['Jun 28 Wonders of the Night\nJoin a ranger for a night hike to find what goes bump in the night!\nRay Roberts Lake State Park - Isle Du Bois Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-isle-du-bois-unit/wonders-of-the-night-2'], ['Jun 29 Guided Nature Hike\nLearn about the local flora and fauna with a Texas Master Naturalist volunteer.\nRay Roberts Lake State Park - Isle Du Bois Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-isle-du-bois-unit/master-naturalist-guided-hike-5'], ['Jun 29 Hike with a Ranger!\nHike with a Ranger through Dogwood Canyon.\nRay Roberts Lake State Park - Johnson Branch Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-johnson-branch-unit/dogwood-canyon-hike-2'], ["Jun 29 Pond Dipping\nLet's explore the pond life!\nRay Roberts Lake State Park - Johnson Branch Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-johnson-branch-unit/pond-dipping"], ['Jun 29 Arts in the Park\nTry your hand at watercolors!\nRay Roberts Lake State Park - Johnson Branch Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-johnson-branch-unit/arts-in-the-park-1']]
['Jun 29 Guided Nature Hike\nLearn about the local flora and fauna with a Texas Master Naturalist volunteer.\nRay Roberts Lake State Park - Isle Du Bois Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-isle-du-bois-unit/master-naturalist-guided-hike-5']
False
not save
[["Jun 28 Dutch Oven Cookin'\nJoin us for a tasty cooking lesson!\nRay Roberts Lake State Park - Isle Du Bois Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-isle-du-bois-unit/dutch-oven-breakfast"], ['Jun 28 Friday Night Frog Talk\nCome see our green, long-legged friends!\nRay Roberts Lake State Park - Johnson Branch Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-johnson-branch-unit/friday-night-frog-talk-8'], ['Jun 28 Wonders of the Night\nJoin a ranger for a night hike to find what goes bump in the night!\nRay Roberts Lake State Park - Isle Du Bois Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-isle-du-bois-unit/wonders-of-the-night-2'], ['Jun 29 Guided Nature Hike\nLearn about the local flora and fauna with a Texas Master Naturalist volunteer.\nRay Roberts Lake State Park - Isle Du Bois Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-isle-du-bois-unit/master-naturalist-guided-hike-5'], ['Jun 29 Hike with a Ranger!\nHike with a Ranger through Dogwood Canyon.\nRay Roberts Lake State Park - Johnson Branch Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-johnson-branch-unit/dogwood-canyon-hike-2'], ["Jun 29 Pond Dipping\nLet's explore the pond life!\nRay Roberts Lake State Park - Johnson Branch Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-johnson-branch-unit/pond-dipping"], ['Jun 29 Arts in the Park\nTry your hand at watercolors!\nRay Roberts Lake State Park - Johnson Branch Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-johnson-branch-unit/arts-in-the-park-1']]
['Jun 29 Hike with a Ranger!\nHike with a Ranger through Dogwood Canyon.\nRay Roberts Lake State Park - Johnson Branch Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-johnson-branch-unit/dogwood-canyon-hike-2']
False
not save
[["Jun 28 Dutch Oven Cookin'\nJoin us for a tasty cooking lesson!\nRay Roberts Lake State Park - Isle Du Bois Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-isle-du-bois-unit/dutch-oven-breakfast"], ['Jun 28 Friday Night Frog Talk\nCome see our green, long-legged friends!\nRay Roberts Lake State Park - Johnson Branch Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-johnson-branch-unit/friday-night-frog-talk-8'], ['Jun 28 Wonders of the Night\nJoin a ranger for a night hike to find what goes bump in the night!\nRay Roberts Lake State Park - Isle Du Bois Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-isle-du-bois-unit/wonders-of-the-night-2'], ['Jun 29 Guided Nature Hike\nLearn about the local flora and fauna with a Texas Master Naturalist volunteer.\nRay Roberts Lake State Park - Isle Du Bois Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-isle-du-bois-unit/master-naturalist-guided-hike-5'], ['Jun 29 Hike with a Ranger!\nHike with a Ranger through Dogwood Canyon.\nRay Roberts Lake State Park - Johnson Branch Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-johnson-branch-unit/dogwood-canyon-hike-2'], ["Jun 29 Pond Dipping\nLet's explore the pond life!\nRay Roberts Lake State Park - Johnson Branch Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-johnson-branch-unit/pond-dipping"], ['Jun 29 Arts in the Park\nTry your hand at watercolors!\nRay Roberts Lake State Park - Johnson Branch Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-johnson-branch-unit/arts-in-the-park-1']]
["Jun 29 Pond Dipping\nLet's explore the pond life!\nRay Roberts Lake State Park - Johnson Branch Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-johnson-branch-unit/pond-dipping"]
False
not save
[["Jun 28 Dutch Oven Cookin'\nJoin us for a tasty cooking lesson!\nRay Roberts Lake State Park - Isle Du Bois Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-isle-du-bois-unit/dutch-oven-breakfast"], ['Jun 28 Friday Night Frog Talk\nCome see our green, long-legged friends!\nRay Roberts Lake State Park - Johnson Branch Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-johnson-branch-unit/friday-night-frog-talk-8'], ['Jun 28 Wonders of the Night\nJoin a ranger for a night hike to find what goes bump in the night!\nRay Roberts Lake State Park - Isle Du Bois Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-isle-du-bois-unit/wonders-of-the-night-2'], ['Jun 29 Guided Nature Hike\nLearn about the local flora and fauna with a Texas Master Naturalist volunteer.\nRay Roberts Lake State Park - Isle Du Bois Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-isle-du-bois-unit/master-naturalist-guided-hike-5'], ['Jun 29 Hike with a Ranger!\nHike with a Ranger through Dogwood Canyon.\nRay Roberts Lake State Park - Johnson Branch Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-johnson-branch-unit/dogwood-canyon-hike-2'], ["Jun 29 Pond Dipping\nLet's explore the pond life!\nRay Roberts Lake State Park - Johnson Branch Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-johnson-branch-unit/pond-dipping"], ['Jun 29 Arts in the Park\nTry your hand at watercolors!\nRay Roberts Lake State Park - Johnson Branch Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-johnson-branch-unit/arts-in-the-park-1']]
['Jun 29 Arts in the Park\nTry your hand at watercolors!\nRay Roberts Lake State Park - Johnson Branch Unit https://tpwd.texas.gov/calendar/ray-roberts-lake-johnson-branch-unit/arts-in-the-park-1']
False
not save
我已经打印了列表和每次尝试要比较的项目以进行故障排除,但是我不明白为什么每次都会出现错误? 在我看来,每个比较都应该是正确的,因为存在列表对象...
答案 0 :(得分:0)