在网页上搜索一个词

Search for a word on webpage

我发现了一些关于这个主题的帖子,试过了,但无法正常工作。

我的代码是:

#!/usr/bin/python
# -*- coding: utf-8 -*-

# Tested Python version: 2.7.12
#
# Run "./script.py [inputfile.txt] [outputfile.txt]"
#
# Exit codes:
# 1 - Python version not tested
# 2 - Wrong number command-line arguments
# 3 - Input file, with this name, does not exist
# 4 - Output file, with this name, already exists
# 5 - Problem with input file
# 6 - Problem with output file

import os, sys
import urllib2, re

# Check python version
req_version = (2, 7)

if not sys.version_info[:2] == req_version:
     print '...'
     print 'Not tested Python version (2.7).'
     print 'Your Python version: ', sys.version_info[:2]
     print '...'
     sys.exit(1)

# Check command-line arguments
if len(sys.argv) < 3:
     print '...'
     print 'Missing command-line argument(s).'
     print 'Argument list:', str(sys.argv)
     print '...'
     sys.exit(2)

# Check if files exist
if not os.path.exists(sys.argv[1]):
     print '...'
     print 'Input file %s was not found.' % sys.argv[1]
     print '...'
     sys.exit(3)

if os.path.exists(sys.argv[2]):
     print '---'
     print 'Output file %s already exists.' % sys.argv[2]
     print '---'
     sys.exit(4)

# Read input file line by line, make a list of URL-s and write the
# results to output file
inputfile = sys.argv[1]
outputfile = sys.argv[2]

print '---'
print 'Reading input file %s ..'  % inputfile
print '---'

results = []

try:
     with open(inputfile, 'r') as in_f:

         for line in in_f:

             url = line.strip().split(',')[0]
             word = line.strip().split(',')[1]
             site = urllib2.urlopen(url).read()

             print 'Found "%s" on "%s" ->' % (word, url)

             # matches = re.search(word)
             # if re.search(word, url):
             # if len(matches) == 0:
             if site.find(word) != -1:
                 print 'YES'
                 results.append('.'.join(url, word + ' YES')))
             else:
                 print 'NO'
                 results.append('.'.join(url, word + ' NO')))
except:
     print 'Error reading the file'
     sys.exit(5)

#if not inputfile.closed:
#     inputfile.close()
print '>>>' + inputfile + ' closed: ' + inputfile.closed

print '...'
print 'Writing results to output file %s ..' % outputfile
print '...'

try:
     with open(outputfile, 'w'):
         for item in results:
             outputfile.write((results) + '\n')
             print '>>>' + outputfile.read()
except:
     print 'Error writing to file'
     sys.exit(6)

#if not outputfile.closed:
#     outputfile.close()
print '>>>' + outputfile + ' closed: ' + outputfile.closed

print ''
print '>>> End of script <<<'
print ''

当我 运行 ./script.py inputfile_name.txt outputfile_name.txt 时,我从终端读取输入文件得到 except:

...
Reading input file inputfile_name txt ..
...
Error reading the file

有人能指出我的代码中可能存在的错误吗?想不通。

编辑: 将变量 (url, word, site) 移动到 'for' 块下并在之后添加打印。该脚本确实打印了 url 字的第一行,但不打印 "Found ...." % 字,之后是 url。如果我删除打印 url,那么脚本会立即给出除错误之外的错误。
EDIT2: 根据用户 Oluwafemi Sule 的建议进行了更改。该脚本一直有效,直到输入文件在 url(句子)之后有多个单词,然后它给出例外。

您代码中的 错误 是由于附加到 results 列表的参数数量不正确。

results.append(url, word + ' YES')

可以写成附加 url 的连接字符串,由 , 分隔的单词和判断:

results.append(','.join((url, word, 'YES')))

奖金:

您的代码中可以更改的内容

以下代码块:

url = line.strip().split(',')[0]
word = line.strip().split(',')[1]

可以改写为:

url, word = line.strip().split(',') 

从分割线保存两次

可以删除以下块,因为 上下文管理器 隐式处理文件关闭。

if not inputfile.closed:
     inputfile.close()
print '>>>' + inputfile + ' closed: ' + inputfile.closed

if not outputfile.closed:
     outputfile.close()
print '>>>' + outputfile + ' closed: ' + outputfile.closed

最后,out_f 没有写入 。这是在 string.

上调用写的潜在 AttributeError