我一直得到语法错误python-3.6

时间:2018-02-22 06:17:39

标签: python-3.x

我一直在第32行遇到语法错误,请帮忙。 我插入了一个User-Agent并且它一直拒绝它

import urllib.request
import urllib.parse

x = urllib.request.urlopen('https://www.google.com/')

print(x.read())

url = 'http://pythonprogramming.net'
values = {'s':'basic',
      'submit':'search'}

data = urllib.parse.urlencode(values)
data = data.encode('utf-8')
req = urllib.request.Request(url,data)
resp = urllib.request.urlopen(req)
respData = resp.read()

print(respData)

try:
    x = urllib.request.urlopen('https://www.google.com/search?q=test')

    print(x.read())

except Exception as e:
    print(str(e))

try:
    url = 'https://www.google.com/search?q=test'
#In line 32 i keep getting a syntax error
    headers = {}

我在这里遇到语法错误         headers = ['User-Agent'] ='Mozilla / 5.0(X11; Linux i686)     为AppleWebKit / 537        (KHTML,与Geko一样)Chrome / 24.0.1312.27 Safari / 537.17'         req = urllib.request.Request(url,headers = headers)         resp = urllib.request.urlopen(req)         respData = resp.read()

    saveFile = open('withHeaders.txt','w')
    saveFile.write(str(respData))
    saveFile.close()

except Exception as e:
    print(str(e))

0 个答案:

没有答案