python爬虫入门(解析)

 根据上一篇的转载进行的总结:

python爬虫的主要步骤

1.访问网站,获取html数据

2.读取html数据,解析数据,取出你想要的值

   解析数据使用beautifulsoup

解析数据的步骤详解:

1. 用beautifulsoup将html数据解析成一个对象

    

soup = BeautifulSoup(html_text, "html.parser")  # 创建BeautifulSoup对象

2.通过soup对象,获取标签内容

    myHead=soup.head    #获取第一个<head>标签

    myBody=soup.body     #获取第一个<body>标签

    myBody=soup.b           #获取第一个<b>标签

    myPara= day.find_all('p')      # 获取所有p标签

3.通过标签对象,获取文本

   text=myPara.string

4.div嵌套div,不好定位,就用find_all获取

data = body.find_all('div', {'class': 'weather_li_left'})
print(data)

 5.获取标签下的所有 <li> 标签,并且输出子标签的文本

     

child=parent.find_all('li')
for text in child:
    print(text.string)

猜你喜欢

转载自542255641.iteye.com/blog/2397385