依然爬取创科实验室网站中讲座的信息(只爬标题,其它同)

但技术上采用requests+正则表达式

 

思想:

#通过正则表达式,获取讲座标题 规则:<h3>中文字符出现4次 任意字符</h3>

m = str(re.findall('<h3>[\u4e00-\u9fff]{4}.+</h3>',html))

# str转换为字符,分割两次得到讲座标题

n = m.split(':')[1].split('<')[0]

n

结果:

python爬虫(五):实战 【3. 使用正则来爬创客实验室】

 

完整代码:

import requests

import re

titlelist = []

# 获取页面

for i in range(1,17):

url = 'http://127.0.0.1/lab/lectureContent/' + str(i)

r = requests.get(url)

r.encoding = r.apparent_encoding

html = r.text

#通过正则表达式,获取讲座标题 规则:<h3>中文字符出现4次 任意字符</h3>

j = str(re.findall('<h3>[\u4e00-\u9fff]{4}.+</h3>',html))

# str转换为字符,分割两次得到讲座标题

m = j.split(':')[1].split('<')[0]

#通过正则表达式,获取报告人 规则:<h5>中文字符出现3次 任意字符</h5>

k = str(re.findall('<h5>[\u4e00-\u9fff]{3}.+</h5>',html))

# str转换为字符,同样分割两次

n = k.split(':')[1].split('<')[0]

titlelist.append([m,n])

titlelist

 

结果:

python爬虫(五):实战 【3. 使用正则来爬创客实验室】

 

 

也可输出一张表,放在D盘

# 输出为表

import pandas as pd

table = pd.DataFrame(data=titlelist,columns=['讲座标题','报告人'])

table.to_csv('D:/2.csv',index=0)

python爬虫(五):实战 【3. 使用正则来爬创客实验室】

 

相关文章:

  • 2022-12-23
  • 2021-11-14
  • 2021-11-12
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-22
  • 2022-12-23
猜你喜欢
  • 2022-01-09
  • 2022-12-23
  • 2021-07-29
  • 2021-05-08
  • 2021-11-11
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案