今天主要完成了根据爬取的txt文档,从百度分类从信息科学类爬取百度词条信息,并写入CSV格式文件。

txt格式文件如图: 为自己爬取内容分词后的结果。

代码如下:

 import requests
from bs4 import BeautifulSoup
import csv
import io
import re url="https://baike.baidu.com/item/"
id=1
patton=re.compile(r'.*信息科学分类.*|.*软件.*|.*科技产品.*|.*公司.*|.*互联网人物.*|.*互联网.*|.*科技术语.*|.*技术.*|.*网站.*') #写入表头
def Head():
with open('E:/bdbk2.csv', 'w', encoding='utf-8', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(["序号", "名称", "属性", "内容", "网址"]) def read():
f = open('E:/bdbk2.csv', 'a+', encoding='utf-8', newline='')
csv_writer = csv.writer(f)
global url
f=open("E:/word5.txt",'r+',encoding="utf-8")
for line in f:
url=url+line.rstrip("\n")
try:
global id, name, nature, content, tag
kv = {'user-agent': 'Mozilla/5.0'}
r = requests.get(url, headers=kv)
r.encoding = "utf-8"
demo = r.text
soup = BeautifulSoup(demo, "html.parser")
print(url)
# print(soup.prettify())
tag = soup.find_all("dd", {"id": "open-tag-item"})[0].get_text().replace("(", "").replace(")",
"").strip().replace(
"\n", "")
name = soup.find_all("h1")[0].get_text().strip()
nature = soup.find_all("h2")[0].get_text().replace("(", "").replace(")", "").strip()
if nature == '目录':
nature = tag
content = soup.find_all("div", {"class": "lemma-summary"})[0].get_text().strip().rstrip("]").lstrip("[")
if name != "百度百科错误页" and nature != "目录" and len(patton.findall(tag)) != 0:
print("序号:" + str(id))
print("名称:" + name)
print("属性:" + nature)
print("内容:" + content)
print("网址:" + url)
csv_writer.writerow([str(id), name, nature, content, url])
id += 1
except:
print("出错!")
url = "https://baike.baidu.com/item/"
f.close() if __name__=="__main__":
Head()
read()

假期学习【十一】Python爬取百度词条写入csv格式 python 2020.2.10的更多相关文章

  1. 爬虫实战(一) 用Python爬取百度百科

    最近博主遇到这样一个需求:当用户输入一个词语时,返回这个词语的解释 我的第一个想法是做一个数据库,把常用的词语和词语的解释放到数据库里面,当用户查询时直接读取数据库结果 但是自己又没有心思做这样一个数 ...

  2. Python——爬取百度百科关键词1000个相关网页

    Python简单爬虫——爬取百度百科关键词1000个相关网页——标题和简介 网站爬虫由浅入深:慢慢来 分析: 链接的URL分析: 数据格式: 爬虫基本架构模型: 本爬虫架构: 源代码: # codin ...

  3. python 爬取百度url

    #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2017-08-29 18:38:23 # @Author : EnderZhou (z ...

  4. java 如何爬取百度百科词条内容(java如何使用webmagic爬取百度词条)

    这是老师所布置的作业 说一下我这里的爬去并非能把百度词条上的内容一字不漏的取下来(而是它分享链接的一个主要内容概括...)(他的主要内容我爬不到 也不想去研究大家有好办法可以call me) 例如 互 ...

  5. python爬取百度贴吧帖子

    最近偶尔学下爬虫,放上第二个demo吧 #-*- coding: utf-8 -*- import urllib import urllib2 import re #处理页面标签类 class Too ...

  6. python爬取百度搜索结果ur汇总

    写了两篇之后,我觉得关于爬虫,重点还是分析过程 分析些什么呢: 1)首先明确自己要爬取的目标 比如这次我们需要爬取的是使用百度搜索之后所有出来的url结果 2)分析手动进行的获取目标的过程,以便以程序 ...

  7. 使用python爬取百度贴吧内的图片

    1. 首先通过urllib获取网页的源码 # 定义一个getHtml()函数 def getHtml(url): try: page = urllib.urlopen(url) # urllib.ur ...

  8. python 爬取百度云资源

    pan1 1 import urllib.request 2 import re 3 import random 4 5 def get_source(key): 6 7 print('请稍等,爬取中 ...

  9. python爬取百度翻译返回:{'error': 997, 'from': 'zh', 'to': 'en', 'query 问题

    解决办法: 修改url为手机版的地址:http://fanyi.baidu.com/basetrans User-Agent也用手机版的 测试代码: # -*- coding: utf-8 -*- & ...

随机推荐

  1. codewars--js--Find The Parity Outlier

    问题描述:(找出奇数数组中唯一的偶数,或是偶数数组中唯一的奇数) You are given an array (which will have a length of at least 3, but ...

  2. 关于使用kms时遇到的there is nothing to do here

    我讲一下我自己的解决方法 ------------------------------------ 这个错误还是很明显的,没事干,如果不是kms的锅的话,那么就是字面上的问题了 ----------- ...

  3. ArcGIS Engine开发碰到问题及解决方式

    1.问题描述——运行提示:ArcGIS version not specified. You must call RuntimeManager.Bind before creating any Arc ...

  4. nCompass-解决方案介绍

    nCompass-解决方案介绍 1.  IT运维的现状及痛点 业务部门投诉系统不可用,各个部门盘查: 网络是通的:系统资源正常:应用进程状态都是正常的:数据库日志中也没有报错 运维被动: 80%的故障 ...

  5. Java获取IP地址,IpUtils工具类,Java IP地址获取

    ================================ ©Copyright 蕃薯耀 2020-01-17 https://www.cnblogs.com/fanshuyao/ import ...

  6. .net core 3.0一个记录request和respose的中间件

    参考资料 https://www.cnblogs.com/wybin6412/p/10944077.html RequestResponseLog.cs using System; using Sys ...

  7. C#设计模式学习笔记:(2)工厂方法模式

    本笔记摘抄自:https://www.cnblogs.com/PatrickLiu/p/7567880.html,记录一下学习过程以备后续查用. 一.引言 接上一篇C#设计模式学习笔记:简单工厂模式( ...

  8. Umi 小白纪实(二)—— model 的注册与使用

    Umi 通常会搭配 Dva 使用,用于管理页面状态和逻辑 一.注册 model 首先需要在 .umirc.js 中启用 dva 插件 export default { plugins: [ ['umi ...

  9. Spring的BeanFactory和FactoryBean

    官方定义 BeanFactory:Spring Bean容器的根接口 FactoryBean:各个对象的工厂接口,如果bean实现了这个接口,它将被用作对象的工厂,而不是直接作为bean实例. 源码解 ...

  10. private、public、this关键字

    private关键字 概念:私有的,一种权限修饰符,用来修饰类的成员 特点:被修饰的成员只能在本类中访问 用法: - 1. private 数据类型 变量名: - 2. private 返回值类型 方 ...