这个是抓取html
def getHtml(url):
re = requests.get(url)
return re.text
index = getHtml(url)
index
这个是解析html的方法
def parseHtml(html):
soup = BeautifulSoup(index,'html.parser')
#soup
lessonList= soup.find('div',class_='codecamplist-catalog').find_all('a')
return lessonList
lessonList = parseHtml(index)
lessonList
最后得到的lessonList 是bs4.element.ResultSet 格式
开始学习HTML标签,
HTML 学习h2标签,
HTML 学习p标签,
删除HTML的注释]
请问一下这样的格式的数据怎么解析呀
目标是把里面的链接和title 保存成csv格式
对应的Tag格式的数据只能找到第一个,使用Find_all方法又会报错。
def getLesson(lessonList):
for i in lessonList:
lesson={}
try:
lesson['title'] = i.find('a')['href'].lstrip('//')
lesson['name']= i.find('a')['title']
except:
print('error')
return lesson
getLesson(lessonList)
# 当上面是 lessonList= soup.find_all('div',class_='codecamplist-catalog')
# .find_all('a') 时为什么只能输出一条呢
结果
{'name': 'Say Hello to HTML Element',
'title': 'www.w3cschool.cn/codecamp/say-hello-to-html-element.html'}