# -*- coding: utf-8 -*
import requests
import re
from bs4 import BeautifulSoup
import MySQLdb
#数据库连接
conn =MySQLdb.connect(host='192.168.0.129',user='root',passwd='123456',db='take_data',charset='utf8')
cur= conn.cursor()
#获取高考省份名字
url="https://www.eol.cn/e_html/gk/tjxk/index.shtml"
headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'}
data= requests.get(url, headers=headers).content
s = re.findall(r"<title>(.*)新", data)
gkcs=s[0]
url =" https://www.eol.cn/e_html/gk/tjxk/js/index.js"
URL ="https://www.eol.cn/e_html/gk/tjxk/html/html_"
data =requests.get(url, headers=headers).content
s = re.findall(r"html_(.*)'\r", data)
for i in s:
url=URL+i
data = requests.get(url, headers=headers).content
soup = BeautifulSoup(data, "html.parser")
ss = soup.find_all('tr')
for tr in ss:
tds=tr.find_all("td")
year=tds[0].string
city = tds[1].string
school = str(tds[2].string).replace(" ", "")
cenci = tds[3].string
tech=str(tds[4].string).replace(" ", "")
kemu = tds[5].string
cur.execute("insert into aaaaaa values (%s,%s,%s,%s,%s,%s,%s)",(gkcs, year, city, school, cenci, tech, kemu))
#天津省的吉林JS文件有问题单独处理
if str(city)=="吉林":
continue
ssss = str(kemu)
if ssss.find("/")>=0:
aa=ssss.split("/")
if len(aa) == 2:
for i in aa:
cur.execute("insert into aaaaaa values (%s,%s,%s,%s,%s,%s,%s)",
(gkcs, year, city, school, cenci, tech, i))
if len(aa) ==3:
a = aa[0] +'/'+ aa[1]
cur.execute("insert into aaaaaa values (%s,%s,%s,%s,%s,%s,%s)",
(gkcs, year, city, school, cenci, tech, a))
b = aa[1] +'/'+ aa[2]
cur.execute("insert into aaaaaa values (%s,%s,%s,%s,%s,%s,%s)",
(gkcs, year, city, school, cenci, tech, b))
c = aa[0] +'/'+ aa[2]
cur.execute("insert into aaaaaa values (%s,%s,%s,%s,%s,%s,%s)",
(gkcs, year, city, school, cenci, tech, c))
for i in aa:
cur.execute("insert into aaaaaa values (%s,%s,%s,%s,%s,%s,%s)",
(gkcs, year, city, school, cenci, tech, i))
if ssss.find("+")>=0:
aa = ssss.split("+")
if len(aa) == 2:
for i in aa:
cur.execute("insert into aaaaaa values (%s,%s,%s,%s,%s,%s,%s)",
(gkcs, year, city, school, cenci, tech, i))
if len(aa) == 3:
a = aa[0] + '+' + aa[1]
cur.execute("insert into aaaaaa values (%s,%s,%s,%s,%s,%s,%s)",
(gkcs, year, city, school, cenci, tech, a))
b = aa[1] + '+' + aa[2]
cur.execute("insert into aaaaaa values (%s,%s,%s,%s,%s,%s,%s)",
(gkcs, year, city, school, cenci, tech, b))
c = aa[0] + '+' + aa[2]
cur.execute("insert into aaaaaa values (%s,%s,%s,%s,%s,%s,%s)",
(gkcs, year, city, school, cenci, tech, c))
for i in aa:
cur.execute("insert into aaaaaa values (%s,%s,%s,%s,%s,%s,%s)",
(gkcs, year, city, school, cenci, tech, i))
cur.close()
conn.commit()
conn.close()
爬取天津省高考数据
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- ---------------------------------------------------------...