python 批量保存网页中的超链接网址源代码

本篇博客介绍了一个简单的Python爬虫程序,该程序使用urllib2库抓取指定网页范围内的链接,并将这些链接保存到本地文件中。通过正则表达式匹配网页中的特定格式链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import urllib2
import time
import re
#connect to a URL
f1=open('all2.txt','a')
for page in range(5,194):
    #url= "https://www.hac-ker.net/search.php?var=That%20is%20me&page="+str(page)
    url= "http://www.example.com/archive?page="+str(page)
    website = urllib2.urlopen(url,timeout = 10)
    #read html code
    html = website.read()
    #use re.findall to get all the links
    #links = re.findall('"((http)s?://.*?)"', html)
    links = re.findall('>((http)s?://.*?)<', html)
    #ti=time.strftime('%y-%m-%d %H:%M:%S',time.localtime(time.time()))
    #f1.write(ti)
    #f1.write("\n\n")
    for i,b in links:
        f1.write(i)
        f1.write("\n")
    page+=1
    print page
    print "\n"
f1.close()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值