python+xpath+requests爬取维基百科历史上的今天
import requests
import urllib.parse
import datetime
from lxml import etree fhout = open("result.txt", 'a') baseurl = 'https://zh.wikipedia.org/wiki/' begin_date = datetime.datetime.strptime('2016-01-01', "%Y-%m-%d")
contents=[]
for i in range(196,366):
content = []
mid_date = begin_date + datetime.timedelta(days=i)
thedate = str(mid_date.month) + '月' + str(mid_date.day) + '日'
print(thedate)
urlthedate = urllib.parse.quote(thedate)
url = baseurl + urlthedate
print(url)
html = requests.get(url).text.encode("utf-8") tree = etree.HTML(html)
ul = tree.xpath('//li[@class="toclevel-1 tocsection-1"]/ul/li/a/span[@class="toctext"]/text()')
num = len(ul)
fhout.write("data_"+str(mid_date.month)+"_"+str(mid_date.day)+"=[")
for i in range(num, 0, -1):
records = tree.xpath('//div[@id="mw-content-text"]/div[@class="mw-parser-output"]/ul['+ str(i) +']/li[descendant-or-self::text()]')
ulen = len(records)
for j in range(ulen-1,-1,-1):
content.append(records[j].xpath('string(.)'))
fhout.write("'"+records[j].xpath('string(.)')+"'\n")
fhout.write("]\n")
print(content)
contents.append(content) fhout.close()
python+xpath+requests爬取维基百科历史上的今天的更多相关文章
- 从0开始学爬虫8使用requests/pymysql和beautifulsoup4爬取维基百科词条链接并存入数据库
从0开始学爬虫8使用requests和beautifulsoup4爬取维基百科词条链接并存入数据库 Python使用requests和beautifulsoup4爬取维基百科词条链接并存入数据库 参考 ...
- python+selenium+requests爬取我的博客粉丝的名称
爬取目标 1.本次代码是在python2上运行通过的,python3的最需改2行代码,用到其它python模块 selenium 2.53.6 +firefox 44 BeautifulSoup re ...
- python+selenium+requests爬取qq空间相册时遇到的问题及解决思路
最近研究了下用python爬取qq空间相册的问题,遇到的问题及解决思路如下: 1.qq空间相册的访问需要qq登录并且需是好友,requests模块模拟qq登录略显麻烦,所以采用selenium的dri ...
- python简单爬虫爬取百度百科python词条网页
目标分析:目标:百度百科python词条相关词条网页 - 标题和简介 入口页:https://baike.baidu.com/item/Python/407313 URL格式: - 词条页面URL:/ ...
- Python使用requests爬取一个网页并保存
#导入 requests模块import requests #设置请求头,让网站监测是浏览器 headers = { 'user-agent': 'Mozilla/5.0 (Windows NT 6. ...
- 爬取维基百科人物介绍,并使用pymysql存储到数据库
代码如下: from urllib.request import urlopen from bs4 import BeautifulSoup import re import datetime imp ...
- Python转页爬取某铝业网站上的数据
天行健,君子以自强不息:地势坤,君子以厚德载物! 好了废话不多说,正式进入主题,前段时间应朋友的请求,爬取了某铝业网站上的数据.刚开始呢,还是挺不愿意的(主要是自己没有完整的爬取过网上的数据哎,即是不 ...
- jQuery请求维基百科[历史上的今天]
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- python xpath图片爬取
import requests from urllib.request import urlretrieve from lxml import etree headers = { 'User-Agen ...
随机推荐
- 检测和删除多余无用的css
本文主要讲解如何检测页面中多余无用的css. 1.chrome浏览器 F12审查元素的Audits 说明:使用Audits,会检测出页面中没有用到的css,需要手动删除多余的css:同时需要说明的是检 ...
- oracel become INDEX UNUSABLE
1. IMPORT PARTITION or conventional path SQL*Loader. 2. Direct-path SQL*Loader leaves affected local ...
- servlet常用操作
servlet常用操作 CreateTime--2017年9月7日09:36:43 Author:Marydon 1.获取当前应用程序对象 需要导入: import javax.servlet.S ...
- 〖Ruby〗Ruby运算符/优先级
优先级 能否重写 运行符 描述 最高 Y [] []= 数组下标 数组元素赋值 Y ** 冥乘 Y ! ~ + - 非 位非 一元加 负号 Y * / % 乘 除 模 Y + - 加 减 Y > ...
- windows执行命令来运行loadrunner录制好的脚本(收藏)
SET M_ROOT=D:Mercury InteractiveMercury LoadRunnerincd %M_ROOT% wlrun.exe -TestPath D:ceshi10Scenari ...
- PHP-PHP.INI常用配置详解
variables_order: 假如为'GPCS'表示系统在定义PHP预定义变量时的顺序是GET,POST,COOKIES,SERVER, 此时$_ENV为空数组, 只要把'E'添加到'GPCS'后 ...
- JavaScript-event参数传递详解
onmouseover="over(event)" onmouseout="out(event)" onclick="change(event)&qu ...
- cxf之GET,POST,PUT,DELETE的区别
GET,POST,PUT,DELETE的区别 注意: 文中有错误地方:关于增改删查正确的描述应为: get对应的是查询post对应的是保存/增加delete对应的是删除put对应的是更新
- python学习笔记——multiprocessing 多进程模块Process
系统自带的fork模块创建的多进程是基于Linux或Unix平台的,而window平台并不支持: python中的multiprocess为跨平台版本的多进程模块,支持子进程.通信和共享数据.执行不同 ...
- 以宽字符形式读整个文件的内容(wifstream的使用)
// TestShlwAPI.cpp : 定义控制台应用程序的入口点.// #include "stdafx.h" #include <iostream>using s ...