python爬虫,个人博客

此博客使用Python进行网页信息爬取。通过urllib.request库打开网页,使用re库的正则表达式匹配功能,分别爬取网页标题、图片地址、内容中的超链接、标题和摘要等信息,展示了基本的网页数据抓取过程。

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

#coding:utf-8
import re
import urllib.request
 
url = "http://www.eastmountyxz.com/"
content = urllib.request.urlopen(url).read().decode('utf-8')   #要加decode和.request

#爬取标题
title = re.findall(r'<title>(.*?)</title>', content)
print (title[0])
 
#爬取图片地址
urls = re.findall(r'src="(.*?)"', content)
for url in urls:
 print (url)
 
#爬取内容
start = content.find(r'<div class="essay">')
end = content.find(r'<div class="essay1">')
page = content[start:end]   
res = r"(?<=href=\").+?(?=\")|(?<=href=\').+?(?=\')"
t1 = re.findall(res, page) #超链接
print (t1[0])
t2 = re.findall(r'<a .*?>(.*?)</a>', page) #标题
print (t2[0])
t3 = re.findall('<p style=.*?>(.*?)</p>', page, re.M|re.S) #摘要
print (t3[0])
print ('')
 
start = content.find(r'<div class="essay1">')
end = content.find(r'<div class="essay2">')
page = content[start:end]   
res = r"(?<=href=\").+?(?=\")|(?<=href=\').+?(?=\')"
t1 = re.findall(res, page) #超链接
print (t1[0])
t2 = re.findall(r'<a .*?>(.*?)</a>', page) #标题
print (t2[0])
t3 = re.findall('<p style=.*?>(.*?)</p>', page, re.M|re.S) #摘要
print (t3[0])
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值