bs4爬虫入门

news/2024/7/19 11:11:27 标签: 爬虫, python
 1 # -*- coding: utf-8 -*-
 2 """
 3 Created on Fri Nov 16 13:35:33 2018
 4 
 5 @author: zhen
 6 """
 7 import urllib
 8 import urllib.request
 9 from bs4 import BeautifulSoup
10 
11 # 设置目标rootUrl,使用urllib.request.Request创建请求
12 rootUrl = "https://www.cnblogs.com/"
13 request = urllib.request.Request(rootUrl)
14 
15 header = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36"
16 # 使用add_header设置请求头,将代码伪装成浏览器
17 request.add_header("User-Agent", header)
18 
19 # 使用urllib.request.urlopen打开页面,使用read方法保存html代码
20 htmlUrl = urllib.request.urlopen(request).read()
21 
22 # 使用BeautifulSoup创建html代码的BeautifulSoup实例,存为beautifulSoup
23 beautifulSoup = BeautifulSoup(htmlUrl)
24 
25 # 获取尾页(对照前一小节获取尾页的内容看你就明白了)
26 total_page = int(beautifulSoup.find("div",class_= "pager").findAll("a")[-2].get_text())
27 
28 list_item = beautifulSoup.findAll("a",class_="titlelnk")
29 for i in list_item: # 遍历所有的内容
30     href = i["href"] # 获取对应的href
31     req = urllib.request.Request(href)
32     req.add_header("User-Agent", header)
33     html = urllib.request.urlopen(req).read()
34     soup = BeautifulSoup(html)
35     # 获取标题
36     titleContent = soup.find("a", id="cb_post_title_url")
37     if titleContent is not None: # 判读是否为空
38         title = titleContent.get_text()   
39         # 获取内容
40         content = soup.find("div").get_text().strip()
41         print(title, "\n=====================================\n", content[1:100])

爬虫结果:

 

 

转载于:https://www.cnblogs.com/yszd/p/9974800.html


http://www.niftyadmin.cn/n/989093.html

相关文章

php 登录对话框

有一点点进步&#xff0c;但以迷茫起来&#xff0c;真不知道自己将来要干什么。。。 <html> <head> <title>Login</title> <style type"text/css"> .pos{ position:absolute; top:35%; left:35%; border:1px solid; border-spacing:30…

Delphi TServerSocket,TClientSocket实现传送文件代码

Delphi TServerSocket,TClientSocket实现传送文件代码 1.建立两个工程Server及Client&#xff0c; 分别放TServerSocket及TClientSocket控件,Demo,Edit控件等。 2.设置TServerSocket name为 SS, ServerType为stNonBlocking,TClientSocket name为cs,ClientType为ctNonBlockin…

一个案例,入门Java动态代理技术

&#xff08;这是我的第三次反馈&#xff09; 首先我们模拟一个场景 我们要找周杰伦唱歌表演 但是我们没办法直接找到周杰伦 周杰伦指派了一个代理人来接待我们&#xff0c;我们只能向代理人提出我们的请求 从这个角度来看是代理人间接地实现了我们的愿望 与此同时对于周…

Markdown使用

2019独角兽企业重金招聘Python工程师标准>>> Markdown是用来写各种说明文档的&#xff0c;比如Readme.md之类的 http://plugins.jetbrains.com/plugin?id5970 在 IntelliJ IDEA 中可以在插件中直接添加&#xff1a; 添加完成后重启&#xff0c;新建一个.md文件&am…

Android Rss阅读器

前言 前几天去北京面试&#xff0c;题目是让我解析一下腾讯的Rss。之前虽然知道xml&#xff0c;但是自己从来没有去学习怎么解析&#xff0c;在网上查一些例子&#xff0c;但是就是没有解析出来。现在看看还蛮好笑的&#xff0c;因为我那时候是使用sax解析xml的&#xff0c;可不…

Python入门第一章--第一节:环境搭建

负一、阅读前注意 本教程使用的Python版本是3.6.5&#xff0c;读者使用的Python版本尽量与本教程一致&#xff0c;最低版本必须是***Python3.5***版本本教程的开发环境是windows 10&#xff0c;其他操作系统亦可本教程的开发IDE是Atom&#xff0c;其他IDE亦可零、Windows 搭建P…

selinux基本操作

selinux转载于:https://blog.51cto.com/13878078/2171883

FeaturedItemProviderAdapterFactory

如果想在viewer里过滤掉一些不需要的元素&#xff0c;除了直接修改XXXItemProvider&#xff08;因为程序的其他地方也会用到这个类&#xff0c;修改它可能会带来问题&#xff09;&#xff0c;另一个办法是通过覆盖XXXItemProviderAdapterFactory&#xff0c;以使用自己继承自XX…