在网址错误中搜索单词

时间:2017-01-04 20:28:43

标签: python python-2.7 web-scraping

我在一个带有唯一ID的文本文件中有一百万个奇怪的网址和搜索字词。我需要打开网址并搜索搜索字符,如果存在则表示为1其他0

输入文件:

"ID" "URL","SearchTerm1","Searchterm2"
"1","www.google.com","a","b"
"2","www.yahoo.com","f","g"
"3","www.att.net","k"
"4" , "www.facebook.com","cs","ee"

代码段:

import urllib2
import re
import csv 
import datetime 
from BeautifulSoup import BeautifulSoup

with open('txt.txt') as inputFile, open ('results.txt','w+') as proc_seqf:
        header = 'Id' + '\t' + 'URL' +  '\t'  
        for i in range(1,3):
            header += 'Found_Search' + str(i) +  '\t'
        header += '\n'
        proc_seqf.write(header)
        for line in inputFile:
            line=line.split(",")
            url = 'http://' + line[1]
            req = urllib2.Request(url, headers={'User-Agent' : "Magic Browser"})
            html_content = urllib2.urlopen(req).read()
            soup = BeautifulSoup(html_content)
            if line[2][0:1] == '"' and line[2][-1:] == '"':
                 line[2] = line[2][1:-1]
            matches = soup(text=re.compile(line[2]))
            #print soup(text=re.compile(line[2]))
            #print matches
            if len(matches) == 0 or line[2].isspace() == True:
                output_1 =0
            else:
                output_1 =1
            #print output_1
            #print line[2]
            if line[3][0:1] == '"' and line[3][-1:] == '"':
                 line[3] = line[3][1:-1]
            matches = soup(text=re.compile(line[3]))
            if len(matches) == 0 or line[3].isspace() == True:
                output_2 =0
            else:
                output_2 =1
            #print output_2
            #print line[3]

            proc_seqf.write("{}\t{}\t{}\t{}\n".format(line[0],url,output_1, output_2))

输出文件:

ID,SearchTerm1,Searchterm2
1,0,1
2,1,0
3,0
4,1,1

代码有两个问题:

  1. 当我一次运行大约200个网址时,它会给我urlopen error [Errno 11004] getaddrinfo failed error

  2. 有没有办法搜索匹配但不完全匹配的内容?

1 个答案:

答案 0 :(得分:2)

  

当我一次运行大约200个网址时,它给了我一个错误的错误[Errno 11004]   getaddrinfo失败错误。

此错误消息告诉您服务器托管的DNS查找 网址失败了。

这不受你的程序控制,但你可以决定如何 处理这种情况。

最简单的方法是捕获错误,记录并继续:

try:
    html_content = urllib2.urlopen(req).read()
except urllib2.URLError as ex:
    print 'Could not fetch {} because of {}, skipping.'.format(url, ex)
    # skip the rest of the loop
    continue

但是,错误可能是暂时的,并且查找会发生错误 如果你以后尝试就可以工作例如,可能是DNS服务器配置为 如果收到的请求在太短的时间内收到太多请求,则拒绝收到的请求 在这种情况下,您可以编写一个函数以在延迟后重试:

import time

class FetchException(Exception):
    pass

def fetch_url(req, retries=5):
    for i in range(1, retries + 1):
        try:
            html_content = urllib2.urlopen(req).read()
        except urllib2.URLError as ex:
            print 'Could not fetch {} because of {}, skipping.'.format(url, ex)
            time.sleep(1 * i))
            continue
        else:
            return html_content
     # if we reach here then all lookups have failed
     raise FetchFailedException() 

# In your main code
try:
    html_content = fetch_url(req)
except FetchFailedException:
    print 'Could not fetch {} because of {}, skipping.'.format(url, ex)
    # skip the rest of the loop
    continue
  

有没有办法搜索匹配但不完全匹配的内容?

如果要将字符串与可选的尾随点匹配,请使用?修饰符。

来自docs

  

使得到的RE匹配前面RE的0或1次重​​复。 AB?将匹配'a'或'ab'。

>>> s = 'Abc In'
>>> m = re.match(r'Abc In.', s)
>>> m is None
True

# Surround `.` with brackets so that `?` only applies to the `.`
>>> m = re.match(r'Abc In(.)?', s)
>>> m.group()
'Abc In'
>>> m = re.match(r'Abc In(.)?', 'Abc In.')
>>> m.group()
'Abc In.'

注意正则表达式模式之前的r字符。这表示raw string。在正则表达式模式中使用原始字符串是一种很好的做法,因为它们可以更容易地处理反斜杠(\)字符,这在正则表达式中非常常见。

所以你可以构造一个正则表达式来匹配可选的尾随点,如下所示:

matches = soup(text=re.compile(r'{}(.)?').format(line[2]))