码迷,mamicode.com
首页 > 编程语言 > 详细

python利用beautifulsoup多页面爬虫

时间:2019-02-09 19:26:32      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:urllib   odi   www.   isp   lock   soup   源码   \n   .com   

利用了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多页面爬虫

标签:urllib   odi   www.   isp   lock   soup   源码   \n   .com   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!