使用Python的requests库,对网页返回状态码status_code进行判断,确认网址是否有效,以support.apple.com为例,遍历其中的部分网址,例如:
https://support.apple.com/zh-cn/HT210643
https://support.apple.com/zh-cn/HT211147
https://support.apple.com/zh-cn/HT204455
根据以上三个url地址可以看出,只需改变最后的几个数字即可获取到不同的地址。通过正则表达式匹配对应地址中的标题,将标题及对应地址信息存储到列表中,并保存至本地。
使用Python源码如下:
import re
import time
import requests
class GetLinksTitles():
def links_titles(self):
links_titles = []
for i in range(211150, 211185):
url = "https://support.apple.com/zh-cn/HT" + str(i)
r = requests.get(url=url)
if r.status_code == 200:
html = r.content.decode('utf-8')
pattern = r'<title lang="zh-CN">(.*?) - Apple 支持</title>'
title = re.findall(pattern, html)
links_titles.append(url)
links_titles.append(title)
print("地址:%s 标题:%s"%(url, title))
else:
print("不存在:%d"%(i))
return links_titles
def save_links_titles(self, links_titles):
file = open('GetLinksTitles' + time.strftime('%Y%m%d%H%M%