当前位置:   article > 正文

Python爬虫问题解决: SyntaxError: invalid syntax_mport urllib.request syntaxerror

mport urllib.request syntaxerror

import urllib.request
import requests
url = 'https://hz.lianjia.com/zufang/'
headers = (
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36'
)
# headers = ('User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36')
request = urllib.request.Request(url,headers = headers)   #构造request请求,加进去了头
response = urllib.request.urlopen(request) #发起请求 得到响应
html = response.read().decode('utf-8')  #把响应读出来,用utf-8进行解码

出现问题: 

 File "C:\Users\DELL\AppData\Local\Temp;\ipykernel_5496\836135880.py", line 5
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36'
                ^
SyntaxError: invalid syntax

import urllib.request
import requests
url = 'https://hz.lianjia.com/zufang/'
headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36'
    }

# headers = ('User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36')
request = urllib.request.Request(url,headers = headers)   #构造request请求,加进去了头
response = urllib.request.urlopen(request) #发起请求 得到响应
html = response.read().decode('utf-8')  #把响应读出来,用utf-8进行解码 

 是括号问题 就离谱

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/101719
推荐阅读
  

闽ICP备14008679号