2016-04-04 7 views
0

私はpython 2.7.8を使用しています。私は本当に何が起こったのかわかりません。すべてうまくいっていたが、突然このエラーが現れた。私は本当にこれが何であるか理解していない。多くを検索するが、解決に失敗する。python 2.7.8IOError:[Errno 2] '\ settings \ ads \ preferences?hl = en'

満杯エラーがある:ここ

IOError: [Errno 2] The system cannot find the path specified: '\\settings\\ads\\preferences?hl=en' 

は私のコードです:

#!/usr/bin/env python 
import re 
import requests 

import urllib 
from bs4 import BeautifulSoup 

def addtoindex(self, url, soup): 
     if self.isindexed (url): return 
     print 'Indexing ' + url 
     # Get the individual words 
     text = self.getTtextonly(url) 
     #print 't',text 
     words = self.separatewords(text) 
     #print 'words',words 
     if stem: words = pracstem.stem(words) 
     # Get the URL id 
     urlid = self.getentryid('googleurllist', 'url', url) 
     #print 'id',urlid 
     # Link each word to this url 
     for i in range(len(words)): 
      word = words[i] 
      # print 'w',word 
      if word in ignorewords: continue 
      wordid = self.getentryid('googlewordlist', 'word', word) 
      #print 'wordid',wordid 

      self.con.execute("insert into googlewordlocation(urlid, wordid, location) values('{0}', '{1}', '{2}')" .format(urlid, wordid, i)) 
      self.con.commit() 


def getTtextonly(self, soup): 
     url = soup 
     #url = "http://www.cplusplus.com/doc/tutorial/program_structure/" 
     html = urllib.urlopen(url).read() # compiler pointing error here 
     soup = BeautifulSoup(html) 

     # kill all script and style elements 
     for script in soup(["script", "style","a","<div id=\"bottom\" >"]): 
      script.extract() # rip it out 

     text = soup.findAll(text=True) 
     return text 

def findfromGoogle(self,a): 

    page = requests.get("https://www.google.com/search?q="+a) 
    soup = BeautifulSoup(page.content) 
    links = soup.findAll("a") 
    for link in links: 
     if link['href'].startswith('/url?q=') \ 
     and 'webcache.googleusercontent.com' not in link['href']: 
      q = link['href'].split('/url?q=')[1].split('&')[0] 
      #self.con.execute("insert into wordlocation(urlid, wordid, location) values(%i, %i, %i)" %(urlid, wordid, i)) 
      # self.con.execute("insert into googleurllist (keyword,url,relevance,textcomplexity)VALUES('{0}','{1}','{2}','{3}')" .format(a,q,'','')) 
      # linkText = self.gettextonly(q) 
      #self.con.commit() 
      print "Records created successfully"; 
      print q 
      self.addtoindex(q,soup) 
      linkText = self.getTtextonly(q) 

エラー:

File "C:\Users\DELL\Desktop\python\s\fyp\Relevancy\M\pyThinSearch\test.py", in getTtextonly 
    html = urllib.urlopen(url).read() 
    File "C:\Python27\lib\urllib.py", line 87, in urlopen 
    return opener.open(url) 
    File "C:\Python27\lib\urllib.py", line 208, in open 
    return getattr(self, name)(url) 
    File "C:\Python27\lib\urllib.py", line 463, in open_file 
    return self.open_local_file(url) 
    File "C:\Python27\lib\urllib.py", line 477, in open_local_file 
    raise IOError(e.errno, e.strerror, e.filename) 
IOError: [Errno 2] The system cannot find the path specified: '\\settings\\ads\\preferences?hl=en' 

私は神経質になっていると私は本当に何であるかエラー理解しませんお問い合わせください....

+0

誰か?????????? – user3162878

答えて

1
q = link['href'].split('/url?q=')[1].split('&')[0] 

qは、相対URLにすることができます。

https://www.google.com/search?q=appleに広告が表示されている場合、href属性が '/ url?q =/settings/ads/preferences'で始まるa要素があります。 the documentation of urllib.urlopenによると

If the URL does not have a scheme identifier, or if it has file: as its scheme identifier, this opens a local file (without universal newlines); otherwise it opens a socket to a server somewhere on the network.

あなたはurllib.urlopenに渡す前に、URLは絶対にするためにurlparse.urljoinを使用する必要があります。

+0

ありがとうございます。 urを勉強した後、答えは最後にこのように言っています: html = urlparse.urljoin(url).read()??その後、私はエラーが発生している? – user3162878

+0

@ user3162878いいえ、絶対URLを 'urllib.urlopen'に渡すべきです。 'q'を絶対にするにはurlparse.urljoinを使います。 – pat

+0

例: 'q = urlparse.urljoin(page.url、link ['href']。split( '/ url?q =')[1] .split( '&')[0])' – pat