鱼C论坛

 找回密码
 立即注册
查看: 830|回复: 4

[已解决]一个语法问题

[复制链接]
发表于 2020-5-3 21:13:57 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 ink_Ocelot 于 2020-5-3 21:13 编辑
  1. import urllib.request
  2. import re
  3. import time

  4. def open_url(url):
  5.     req = urllib.request.Request(url)
  6.     req.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0')
  7.     page = urllib.request.urlopen(req)
  8.     html = page.read().decode('utf-8')

  9.     return html

  10. def get_ip(html):

  11.     #r'(([0,1]?\d?\d│2[0-4]\d│25[0-5])\.{3})(([0,1]?\d?\d│2[0-4]\d│25[0-5])'
  12.     #r'(?:(?:[0,1]?\d?\d|2[0-4]\d|25[0-5])\.){3}(?:[0,1]?\d?\d|2[0-4]\d|25[0-5])'

  13.     p = r'(?:(?:[0,1]?\d?\d|2[0-4]\d|25[0-5])\.){3}(?:[0,1]?\d?\d|2[0-4]\d|25[0-5])'
  14.     iplist = re.findall(p,html)

  15. #    for each in iplist:
  16. #        print(each)

  17. #    for each in iplist:
  18. #        print (each)

  19.     for each in iplist:
  20.         exec('ipwrite.write(\'' + each + '\n\')'

  21. if __name__ == '__main__':
  22.     page_num = int(input('输入获取IP数量(以页面计):')) + 1
  23.     exec('ipwrite = open(\'IP:page1-page' + str(page_num - 1) + '.txt\',\'w\')')
  24.     for i in range(1,page_num):
  25.         exec('url = \'https://www.xicidaili.com/nt/' + str(i) + '\'')
  26.         get_ip(open_url(url))
  27.     ipwrite.close()
  28.     print ('IP爬取完成!')
  29.     time.sleep (5)
复制代码



这个代码一直提示SyntaxError: invalid syntax
标红if __name__ == '__main__':中的':'
各位大佬知道这个问题怎么解决吗?
最佳答案
2020-5-3 22:55:16
本帖最后由 hrp 于 2020-5-3 22:56 编辑
ink_Ocelot 发表于 2020-5-3 21:34
还是返回 SyntaxError: invalid syntax 啊

  1. import re
  2. import time
  3. import urllib.request


  4. def open_url(url):
  5.     req = urllib.request.Request(url)
  6.     req.add_header(
  7.         'User-Agent',
  8.         'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0'
  9.     )
  10.     page = urllib.request.urlopen(req)
  11.     html = page.read().decode('utf-8')
  12.     print(html)

  13.     return html


  14. def get_ip(html):

  15.     #r'(([0,1]?\d?\d│2[0-4]\d│25[0-5])\.{3})(([0,1]?\d?\d│2[0-4]\d│25[0-5])'
  16.     #r'(?:(?:[0,1]?\d?\d|2[0-4]\d|25[0-5])\.){3}(?:[0,1]?\d?\d|2[0-4]\d|25[0-5])'

  17.     p = r'(?:(?:[0,1]?\d?\d|2[0-4]\d|25[0-5])\.){3}(?:[0,1]?\d?\d|2[0-4]\d|25[0-5])'
  18.     iplist = re.findall(p, html)

  19.     #    for each in iplist:
  20.     #        print(each)

  21.     #    for each in iplist:
  22.     #        print (each)

  23.     for each in iplist:
  24.         # exec('ipwrite.write(\'' + each + '\n\')')   # 最初报错的原因是这里少个回头的小括号,但还有其他问题。
  25.         ipwrite.write(each + '\n')  # 是不是想这么写?


  26. if __name__ == '__main__':
  27.     page_num = int(input('输入获取IP数量(以页面计):'))
  28.     ipwrite = open('IP-page1-page' + str(page_num) + '.txt', 'w') # 在win中冒号不能用作文件名!
  29.     for i in range(1, page_num + 1):
  30.         url = 'https://www.xicidaili.com/nt/' + str(i)  # 你是不是想这么写?
  31.         get_ip(open_url(url))
  32.     ipwrite.close()
  33.     print('IP爬取完成!')
  34.     time.sleep(5)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-5-3 21:19:01 | 显示全部楼层
这样试试:

  1. import urllib.request
  2. import re
  3. import time

  4. def open_url(url):
  5.     req = urllib.request.Request(url)
  6.     req.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0')
  7.     page = urllib.request.urlopen(req)
  8.     html = page.read().decode('utf-8')

  9.     return html

  10. def get_ip(html):

  11.     #r'(([0,1]?\d?\d│2[0-4]\d│25[0-5])\.{3})(([0,1]?\d?\d│2[0-4]\d│25[0-5])'
  12.     #r'(?:(?:[0,1]?\d?\d|2[0-4]\d|25[0-5])\.){3}(?:[0,1]?\d?\d|2[0-4]\d|25[0-5])'

  13.     p = r'(?:(?:[0,1]?\d?\d|2[0-4]\d|25[0-5])\.){3}(?:[0,1]?\d?\d|2[0-4]\d|25[0-5])'
  14.     iplist = re.findall(p,html)

  15. #    for each in iplist:
  16. #        print(each)

  17. #    for each in iplist:
  18. #        print (each)

  19.     for each in iplist:
  20.         exec('ipwrite.write(\' + each + \'\n\')')

  21. if __name__ == '__main__':
  22.     page_num = int(input('输入获取IP数量(以页面计):')) + 1
  23.     exec('ipwrite = open(\'IP:page1-page' + str(page_num - 1) + '.txt\',\'w\')')
  24.     for i in range(1,page_num):
  25.         exec('url = \'https://www.xicidaili.com/nt/' + str(i) + '\'')
  26.         get_ip(open_url(url))
  27.     ipwrite.close()
  28.     print ('IP爬取完成!')
  29.     time.sleep (5)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-3 21:20:02 | 显示全部楼层
应该好好锻炼DEBUG技巧了...
  1. import urllib.request
  2. import re
  3. import time

  4. def open_url(url):
  5.     req = urllib.request.Request(url)
  6.     req.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0')
  7.     page = urllib.request.urlopen(req)
  8.     html = page.read().decode('utf-8')

  9.     return html

  10. def get_ip(html):

  11.     #r'(([0,1]?\d?\d│2[0-4]\d│25[0-5])\.{3})(([0,1]?\d?\d│2[0-4]\d│25[0-5])'
  12.     #r'(?:(?:[0,1]?\d?\d|2[0-4]\d|25[0-5])\.){3}(?:[0,1]?\d?\d|2[0-4]\d|25[0-5])'

  13.     p = r'(?:(?:[0,1]?\d?\d|2[0-4]\d|25[0-5])\.){3}(?:[0,1]?\d?\d|2[0-4]\d|25[0-5])'
  14.     iplist = re.findall(p,html)

  15. #    for each in iplist:
  16. #        print(each)

  17. #    for each in iplist:
  18. #        print (each)

  19.     for each in iplist:
  20.         ipwrite.write('' + each + '\n')
  21.         
  22. if __name__ == '__main__':
  23.     page_num = int(input('输入获取IP数量(以页面计):')) + 1
  24.     exec('ipwrite = open(\'IP:page1-page' + str(page_num - 1) + '.txt\',\'w\')')
  25.     for i in range(1,page_num):
  26.         exec('url = \'https://www.xicidaili.com/nt/' + str(i) + '\'')
  27.         get_ip(open_url(url))
  28.     ipwrite.close()
  29.     print ('IP爬取完成!')
  30.     time.sleep (5)
复制代码


在28行那个ipwrite里面,我有个疑问,为啥非要用exec?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-3 21:34:19 | 显示全部楼层
还是返回 SyntaxError: invalid syntax 啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-3 22:55:16 | 显示全部楼层    本楼为最佳答案   
本帖最后由 hrp 于 2020-5-3 22:56 编辑
ink_Ocelot 发表于 2020-5-3 21:34
还是返回 SyntaxError: invalid syntax 啊

  1. import re
  2. import time
  3. import urllib.request


  4. def open_url(url):
  5.     req = urllib.request.Request(url)
  6.     req.add_header(
  7.         'User-Agent',
  8.         'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0'
  9.     )
  10.     page = urllib.request.urlopen(req)
  11.     html = page.read().decode('utf-8')
  12.     print(html)

  13.     return html


  14. def get_ip(html):

  15.     #r'(([0,1]?\d?\d│2[0-4]\d│25[0-5])\.{3})(([0,1]?\d?\d│2[0-4]\d│25[0-5])'
  16.     #r'(?:(?:[0,1]?\d?\d|2[0-4]\d|25[0-5])\.){3}(?:[0,1]?\d?\d|2[0-4]\d|25[0-5])'

  17.     p = r'(?:(?:[0,1]?\d?\d|2[0-4]\d|25[0-5])\.){3}(?:[0,1]?\d?\d|2[0-4]\d|25[0-5])'
  18.     iplist = re.findall(p, html)

  19.     #    for each in iplist:
  20.     #        print(each)

  21.     #    for each in iplist:
  22.     #        print (each)

  23.     for each in iplist:
  24.         # exec('ipwrite.write(\'' + each + '\n\')')   # 最初报错的原因是这里少个回头的小括号,但还有其他问题。
  25.         ipwrite.write(each + '\n')  # 是不是想这么写?


  26. if __name__ == '__main__':
  27.     page_num = int(input('输入获取IP数量(以页面计):'))
  28.     ipwrite = open('IP-page1-page' + str(page_num) + '.txt', 'w') # 在win中冒号不能用作文件名!
  29.     for i in range(1, page_num + 1):
  30.         url = 'https://www.xicidaili.com/nt/' + str(i)  # 你是不是想这么写?
  31.         get_ip(open_url(url))
  32.     ipwrite.close()
  33.     print('IP爬取完成!')
  34.     time.sleep(5)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-18 17:14

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表