如果列表中已经包含current_url
,我想跳过它,但会遇到错误消息。
我的目标是抓取页面,将该网页添加到文本文件中,然后当我重新开始抓取时,我想将要抓取的网页与列表中的网页进行比较。当网页位于时,我要跳过它。
但是弹出此问题,无法将current_url
与列表进行比较:
这段代码:
if cur_url in visited_urls:
整个代码:
visited_urls = 'C:/webdrivers/visited_p_urls_test.txt' # This specific the location of the text file on the PC
cur_url = driver.current_url
# Go to main test website
driver.get('https://www.google.com')
sleep(randint(1,3))
with open(visited_urls, 'a') as filehandle: # This opens a text file with the "Append to (add) mode."
filehandle.write('\n' + cur_url)
# Go to main test website
driver.get('https://adwords.google.com/home/')
sleep(randint(1,3))
with open(visited_urls, 'a') as filehandle: # This opens a text file with the "Append to (add) mode."
filehandle.write('\n' + cur_url)
driver.get('https://adwords.google.com/home/tools/')
sleep(randint(1,3))
with open(visited_urls, 'a') as filehandle: # This opens a text file with the "Append to (add) mode."
filehandle.write('\n' + cur_url)
if cur_url in visited_urls:
print 'I CANNOT comment because I already did before'
else:
print 'I can comment'
with open(visited_urls, 'r') as filehandle: # This opens a text file with the "Read" mode.
filecontent = filehandle.readlines() # readlines reads ALL lines in a text file
for line in filecontent:
print(line)
我收到此错误消息:
TypeError: 'str' object is not callable
答案 0 :(得分:0)
您正在尝试在文件路径(C:/webdrivers/visited_p_urls_test.txt
)中搜索字符串,但是必须在文件内容中进行搜索:
if 'blabla' in open('example.txt').read():
print("true")
在您的情况下:
# open file, read it's content and only then search if cur_url exists already in a file
if cur_url in open(visited_urls).read():
print 'I CANNOT comment because I already did before'
else:
print 'I can comment'