首页 > 编程语言 > 详细

python利用beautifulsoup多页面爬虫

时间:2019-02-09 17:56:12      阅读:222      评论:0      收藏:0      [点我收藏+]

利用了beautifulsoup进行爬虫,解析网址分页面爬虫并存入文本文档:

结果:

技术分享图片

源码:

from bs4 import BeautifulSoup
from urllib.request import urlopen
with open("热门标题.txt","a",encoding="utf-8") as f:
    for i in range(2):
        url = "http://www.ltaaa.com/wtfy-{}".format(i)+".html"
        html = urlopen(url).read()
        soup = BeautifulSoup(html,"html.parser")
        titles = soup.select("div[class = ‘dtop‘ ] a") # CSS 选择器
        for title in titles:
             print(title.get_text(),title.get(‘href‘))# 标签体、标签属性
             f.write("标题:{}\n".format(title.get_text()))

  

 

python利用beautifulsoup多页面爬虫

原文:https://www.cnblogs.com/mm20/p/10357727.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!