美文网首页
Python 学习之路(一)

Python 学习之路(一)

作者: 五彩萝卜 | 来源:发表于2018-02-21 15:46 被阅读8次

#coding:utf-8  #设置文本格式

import requests  # 引入request库

from bs4 import  BeautifulSoup

url = 'http://news.qq.com'

wbdata = requests.get(url).text      #request.get方法获取url

soup = BeautifulSoup(wbdata,'lxml')  

news_titles = soup.select('div > div > em > a')  获取select 的路径 

for n in news_titles:  # news_titles 循环 

        title = n.get_text()

        link = n.get('href')

        data = {

            '标题':title,

            '链接':link,

        }

        print(data)  #数据打印出来 

相关文章

网友评论

      本文标题:Python 学习之路(一)

      本文链接:https://www.haomeiwen.com/subject/stvrzttx.html