試圖找出如何使用if語句,在該語句中,我可以將三到四個單詞分組,以便從CSV文件中省略。在代碼底部,您會看到我卡在:if ('reddit', 'passwords') not in x:
python腳本 - 將單詞分組爲If-Not語句
任何幫助都會很棒。
# import libraries
import bs4
from urllib2 import urlopen as uReq
from bs4 import BeautifulSoup as soup
my_url = 'https://www.reddit.com/r/NHLStreams/comments/71uhwi/game_thread_sabres_at_maple_leafs_730_pm_et/'
# opening up connection, grabbing the page
uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()
# html parsing
page_soup = soup(page_html, "html.parser")
filename = "sportstreams.csv"
f = open(filename, "w")
headers = "Sport Links " + "\n"
f.write(headers)
links = page_soup.select("form a[href]")
for link in links:
href = link["href"]
print(href)
f.write(href + "\n")
with open('sportstreams.csv') as f,open('sstream.csv', "w") as f2:
for x in f:
if ('reddit', 'passwords') not in x: # trying to find multi words to omit
f2.write(x.strip()+'\n')
目前還不清楚你想要什麼'如果(...)不在x'中做。所有的元素都必須從'x'中丟失,或者它們中的任何一個足以觸發'if'? –
我想我的代碼很弱,因爲我試圖用任何包含單詞「reddit」「/ r /」和「/ password」的行來簡化我的結果以省略。這將縮短我的鏈接列表,這對我來說是成功的。 :) –
請編輯您的問題的解釋,使其完成。如果你能夠展示你想要忽略的行與你想要保留的行的具體例子,那將是很好的。 –