程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

Python爬取网页中的链接标题

发布于2019-10-28 19:37     阅读(1006)     评论(0)     点赞(9)     收藏(1)


from bs4 import BeautifulSoup
import requests
import time

t0 = time.perf_counter()
url = "https://www.shiyanlou.com/questions/102676/?utm_source=baidu&utm_medium=cpc&utm_campaign=python&utm_term=keywords&renqun_youhua=417274"
headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36'
}

html = requests.get(url,headers = headers).content

t1 = time.perf_counter()

soup = BeautifulSoup(html,"lxml")
bks = soup.select("#__layout > div > div.body > div.container > div.row > div.col-md-9 > div > div > div.markdown-body > ol > li > a")
i=0

t2 = time.perf_counter()

for bk in bks:
    i = i +1
    print(str(i) , bk.text)
t3 = time.perf_counter()

#各个步骤所耗时间
print ("read:", t1-t0)
print ("soup:", t2-t1)
print ("print:", t3-t2 )


所属网站分类: 技术文章 > 博客

作者:精灵

链接:https://www.pythonheidong.com/blog/article/147419/3deb499f852588fdfae4/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

9 0
收藏该文
已收藏

评论内容:(最多支持255个字符)