python爬虫,从hao123爬取网址信息

最近研究python的爬虫,小小程序,拿下来分享,本人使用python3.7,纯粹兴趣爱好,希望能帮助大家激发兴趣。
从hao123,爬取各种网址信息,代码如下。

import urllib.request
import urllib
import re

#获取网站首页全部内容
url = "http://www.hao123.com"
user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'
req = urllib.request.Request(url, headers={'User-Agent': user_agent})
response = urllib.request.urlopen(req)
content = response.read().decode('utf-8')
#print(content)

#初级筛选
pattern = re.compile('<a.*?href="http://.*?".*?>.*?</a>')
items = re.findall(pattern, content)
for item in items:
pattern_one = re.compile('href=".*?"')
pattern_two = re.compile('.*?</a>')
http = re.findall(pattern_one, item)
name = re.findall(pattern_two, item)
name = name.__str__().replace('</a>', '')
#print(name)
aa = name.rindex('">')
print(name[aa+1:len(name)].replace('\']','').replace('</span>','').replace('>','') + ':' + http.__str__().replace('href=','').replace('"','').replace('\'','').replace('[','').replace(']',''))

结果:

 

直升机大战:http://xyx.hao123.com/game/600523.html
魔方跳跃:http://xyx.hao123.com/game/599918.html
旋转消消乐:http://xyx.hao123.com/game/597970.html
画线篮球2:http://xyx.hao123.com/game/598991.html
自由任意球:http://xyx.hao123.com/game/599342.html
冰娃与火娃:http://xyx.hao123.com/game/600062.html
足球对抗赛:http://xyx.hao123.com/game/233871.html
足球锦标赛:http://xyx.hao123.com/game/192712.html
荣耀之剑:http://xyx.hao123.com/game/600035.html
复仇者联盟:http://xyx.hao123.com/game/599138.html
火柴人跑酷:http://xyx.hao123.com/game/595991.html
熊出没冒险:http://xyx.hao123.com/game/557537.html
大头吃橙子:http://xyx.hao123.com/game/596718.html

 

结果有很多不全部列举

 

转载于:https://www.cnblogs.com/Monster-World/p/9542982.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值