鱼C论坛

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

[已解决]一个语法问题

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

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

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

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

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

    return html

def get_ip(html):

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

    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])'
    iplist = re.findall(p,html)

#    for each in iplist:
#        print(each)

#    for each in iplist:
#        print (each)

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

if __name__ == '__main__':
    page_num = int(input('输入获取IP数量(以页面计):')) + 1
    exec('ipwrite = open(\'IP:page1-page' + str(page_num - 1) + '.txt\',\'w\')')
    for i in range(1,page_num):
        exec('url = \'https://www.xicidaili.com/nt/' + str(i) + '\'')
        get_ip(open_url(url))
    ipwrite.close()
    print ('IP爬取完成!')
    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 啊

import re
import time
import urllib.request


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

    return html


def get_ip(html):

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

    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])'
    iplist = re.findall(p, html)

    #    for each in iplist:
    #        print(each)

    #    for each in iplist:
    #        print (each)

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


if __name__ == '__main__':
    page_num = int(input('输入获取IP数量(以页面计):'))
    ipwrite = open('IP-page1-page' + str(page_num) + '.txt', 'w') # 在win中冒号不能用作文件名!
    for i in range(1, page_num + 1):
        url = 'https://www.xicidaili.com/nt/' + str(i)  # 你是不是想这么写?
        get_ip(open_url(url))
    ipwrite.close()
    print('IP爬取完成!')
    time.sleep(5)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-5-3 21:19:01 | 显示全部楼层
这样试试:
import urllib.request
import re
import time

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

    return html

def get_ip(html):

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

    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])'
    iplist = re.findall(p,html)

#    for each in iplist:
#        print(each)

#    for each in iplist:
#        print (each)

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

if __name__ == '__main__':
    page_num = int(input('输入获取IP数量(以页面计):')) + 1
    exec('ipwrite = open(\'IP:page1-page' + str(page_num - 1) + '.txt\',\'w\')')
    for i in range(1,page_num):
        exec('url = \'https://www.xicidaili.com/nt/' + str(i) + '\'')
        get_ip(open_url(url))
    ipwrite.close()
    print ('IP爬取完成!')
    time.sleep (5)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

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

    return html

def get_ip(html):

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

    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])'
    iplist = re.findall(p,html)

#    for each in iplist:
#        print(each)

#    for each in iplist:
#        print (each)

    for each in iplist:
        ipwrite.write('' + each + '\n')
        
if __name__ == '__main__':
    page_num = int(input('输入获取IP数量(以页面计):')) + 1
    exec('ipwrite = open(\'IP:page1-page' + str(page_num - 1) + '.txt\',\'w\')')
    for i in range(1,page_num):
        exec('url = \'https://www.xicidaili.com/nt/' + str(i) + '\'')
        get_ip(open_url(url))
    ipwrite.close()
    print ('IP爬取完成!')
    time.sleep (5)

在28行那个ipwrite里面,我有个疑问,为啥非要用exec?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-3 21:34:19 | 显示全部楼层
还是返回 SyntaxError: invalid syntax 啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

import re
import time
import urllib.request


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

    return html


def get_ip(html):

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

    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])'
    iplist = re.findall(p, html)

    #    for each in iplist:
    #        print(each)

    #    for each in iplist:
    #        print (each)

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


if __name__ == '__main__':
    page_num = int(input('输入获取IP数量(以页面计):'))
    ipwrite = open('IP-page1-page' + str(page_num) + '.txt', 'w') # 在win中冒号不能用作文件名!
    for i in range(1, page_num + 1):
        url = 'https://www.xicidaili.com/nt/' + str(i)  # 你是不是想这么写?
        get_ip(open_url(url))
    ipwrite.close()
    print('IP爬取完成!')
    time.sleep(5)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-11-27 03:13

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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