标签:pymysql find and main spider __name__ mys date ada
#-*- coding:utf-8 -*-
import requests
import chardet
from bs4 import BeautifulSoup
‘‘‘
import tushare as ts
import pandas as pd
import pymysql
import lxml
import time
import datetime
import os
import random
pymysql.install_as_MySQLdb()
from sqlalchemy import create_engine
from lxml import etree
‘‘‘
‘‘‘
爬取新浪股市雷达
‘‘‘
def sina_Spider():
for page_num in range(1,16):
url = ‘http://finance.sina.com.cn/stockradar/stockradar‘ + str(page_num) + ‘.html‘
headers = {"User-Agent":"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3314.0 Safari/537.36 SE 2.X MetaSr 1.0"}
html = requests.get(url=url,headers=headers)
html.encoding = html.apparent_encoding
soup = BeautifulSoup(html.text,‘lxml‘)
#tr_list = soup.select(‘.tbl_wrap > table > tbody > tr‘)
tr_list1 = soup.find_all(‘tr‘)
for idx, tr in enumerate(tr_list1):
if idx != 0:
tds = tr.find_all(‘th‘)
change_time = tds[0].string
stkcode = tds[1].string
stkname = tds[2].string
type_value = tds[3].string
data =
sql = ‘‘‘insert ignore into stock.***(date,stkcode,stkname,change_time,type_value) values (%s,%s,%s,%s,%s)‘‘‘
print(change_time)
print(stkcode)
print(stkname)
print(type_value)
if __name__ == "__main__":
‘‘‘
ts.set_token(‘ac16b470869c5d82db5033ae9288f77b282d2b5519507d6d2c72fdd7‘)
pro = ts.pro_api()
# 1、数据库参数
conn = pymysql.connect(host=‘127.0.0.1‘, port=3308, user=‘root‘, password=‘abc123‘, db=‘zlcpy_db‘, charset=‘utf8‘)
print(conn)
# 2、创建操作的游标
cursor = conn.cursor()
‘‘‘
# 调用函数
sina_Spider()
标签:pymysql find and main spider __name__ mys date ada
原文地址:https://www.cnblogs.com/Iceredtea/p/11717418.html