爬取网页表格数据至本地
2023-5-18
爬取网站表格数据1.0
1.对http://quote.eastmoney.com/center/gridlist.html#hs_a_board网站进行数据的爬取。后面会试着再往数据库里面写。
2.在代码中,指定D:/spider_file/spider_excel/stock.xls
图1:网站界面
import random
from selenium import webdriver
import time
from lxml import etree
import requests
import xlwt
user_agent = [
"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50",
"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50",
"Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0",
"Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; InfoPath.3; rv:11.0) like Gecko",
"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",
"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1",
"Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1",
"Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.8.131 Version/11.11",
"Opera/9.80 (Windows NT 6.1; U; en) Presto/2.8.131 Version/11.11",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Maxthon 2.0)",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; TencentTraveler 4.0)",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; The World)",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; SE 2.X MetaSr 1.0; SE 2.X MetaSr 1.0; .NET CLR 2.0.50727; SE 2.X MetaSr 1.0)",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; 360SE)",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Avant Browser)",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)",
"Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5",
"Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5",
"Mozilla/5.0 (iPad; U; CPU OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5",
"Mozilla/5.0 (Linux; U; Android 2.3.7; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1",
"MQQBrowser/26 Mozilla/5.0 (Linux; U; Android 2.3.7; zh-cn; MB200 Build/GRJ22; CyanogenMod-7) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1",
"Opera/9.80 (Android 2.3.4; Linux; Opera Mobi/build-1107180945; U; en-GB) Presto/2.8.149 Version/11.10",
"Mozilla/5.0 (Linux; U; Android 3.0; en-us; Xoom Build/HRI39) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13",
"Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; en) AppleWebKit/534.1+ (KHTML, like Gecko) Version/6.0.0.337 Mobile Safari/534.1+",
"Mozilla/5.0 (hp-tablet; Linux; hpwOS/3.0.0; U; en-US) AppleWebKit/534.6 (KHTML, like Gecko) wOSBrowser/233.70 Safari/534.6 TouchPad/1.0",
"Mozilla/5.0 (SymbianOS/9.4; Series60/5.0 NokiaN97-1/20.0.019; Profile/MIDP-2.1 Configuration/CLDC-1.1) AppleWebKit/525 (KHTML, like Gecko) BrowserNG/7.1.18124",
"Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; HTC; Titan)",
"UCWEB7.0.2.37/28/999",
"NOKIA5700/ UCWEB7.0.2.37/28/999",
"Openwave/ UCWEB7.0.2.37/28/999",
"Mozilla/4.0 (compatible; MSIE 6.0; ) Opera/UCWEB7.0.2.37/28/999",
# iPhone 6:
"Mozilla/6.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/8.0 Mobile/10A5376e Safari/8536.25",
]
headers = {'User-Agent': random.choice(user_agent)}
url = 'http://quote.eastmoney.com/center/gridlist.html#hs_a_board'
# 浏览器驱动绝对路径
driver = webdriver.Chrome()
driver.maximize_window()
# 跳转网页
driver.get(url)
time.sleep(1)
source_result = driver.page_source
# response=requests.get(url,headers=headers)
# resp=response.text
html=etree.HTML(source_result)
time.sleep(1)
driver.quit()
#首行的名称字段
field_name=html.xpath('/html/body/div[1]/div[2]/div[2]/div[5]/div/table/thead/tr/th/span/text()')
#序号
stock_id=html.xpath('/html/body/div[1]/div[2]/div[2]/div[5]/div/table/tbody/tr/td[1]/text()')
#代码
stock_code=html.xpath('/html/body/div[1]/div[2]/div[2]/div[5]/div/table/tbody/tr/td[2]/a/text()')
#名称
stock_name=html.xpath('/html/body/div[1]/div[2]/div[2]/div[5]/div/table/tbody/tr/td[3]/a/text()')
#相关链接
stock_link=html.xpath('/html/body/div[1]/div[2]/div[2]/div[5]/div/table/tbody/tr/td[4]/a[1]/@href')
#最新价
stock_newprice=html.xpath('/html/body/div[1]/div[2]/div[2]/div[5]/div/table/tbody/tr/td[5]/span/text()')
#涨跌幅
stock_change=html.xpath('/html/body/div[1]/div[2]/div[2]/div[5]/div/table/tbody/tr/td[6]/span/text()')
#涨跌额
stock_rise_fall=html.xpath('/html/body/div[1]/div[2]/div[2]/div[5]/div/table/tbody/tr/td[7]/span/text()')
#成交量(手)
stock_turnover=html.xpath('/html/body/div[1]/div[2]/div[2]/div[5]/div/table/tbody/tr/td[8]/text()')
datalist=[]
# datalist.append(field_name)
datalist.append(stock_id)
datalist.append(stock_code)
datalist.append(stock_name)
datalist.append(stock_link)
datalist.append(stock_newprice)
datalist.append(stock_change)
datalist.append(stock_rise_fall)
datalist.append(stock_turnover)
print(datalist)
#写入excel
#创建excel表格类型文件
book = xlwt.Workbook(encoding='utf-8',style_compression=0)
#在excel表格类型文件中建立一张sheet表单 cell_overwrite_ok用于确认同一个cell单元是否可以重设值,这里赋值为True就表示可重设值。
sheet = book.add_sheet('沪深京个股',cell_overwrite_ok=True)
#首行字段写入excel中
for i in range(len(field_name[0:8])):
sheet.write(0,i,field_name[i]) #第一个参数是行、第二个参数是列、第三个当然就是col元组值
print(len(datalist))
for i in range(len(field_name[0:8])):
for j in range(len(stock_code)):
# print(len(datalist[i]))
sheet.write(j+1,i,datalist[i][j])
savepath = 'D:/spider_file/spider_excel/stock.xls'
book.save(savepath)
爬取思路:
1.获取首行的字段名
2.获取各个字段对应的数据,写入到excel中
图2:控制台
图3:实际情况
图4:
后续改进:
- 1.5版准备将后面多页(如图4所示)都爬取到。
2.计划将表格宽度设置一下,图3有些字段有些宽度太短,被挡住了。
3.优化一下代码,1.0代码写的稍微有点乱,有点像将功能胡乱拼凑,后续会改进。
2023-5-19
爬取网站表格数据1.5
1.今天实现了多页爬取,将沪深京的所有股的信息爬取下来了
2.今天也改进了昨天的问题,将第三列行宽改变了
代码如下:
import random
from selenium import webdriver
import time
from lxml import etree
import requests
import xlwt
user_agent = [
"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50",
"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50",
"Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0",
"Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; InfoPath.3; rv:11.0) like Gecko",
"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",
"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1",
"Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1",
"Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.8.131 Version/11.11",
"Opera/9.80 (Windows NT 6.1; U; en) Presto/2.8.131 Version/11.11",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Maxthon 2.0)",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; TencentTraveler 4.0)",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; The World)",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; SE 2.X MetaSr 1.0; SE 2.X MetaSr 1.0; .NET CLR 2.0.50727; SE 2.X MetaSr 1.0)",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; 360SE)",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Avant Browser)",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)",
"Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5",
"Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5",
"Mozilla/5.0 (iPad; U; CPU OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5",
"Mozilla/5.0 (Linux; U; Android 2.3.7; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1",
"MQQBrowser/26 Mozilla/5.0 (Linux; U; Android 2.3.7; zh-cn; MB200 Build/GRJ22; CyanogenMod-7) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1",
"Opera/9.80 (Android 2.3.4; Linux; Opera Mobi/build-1107180945; U; en-GB) Presto/2.8.149 Version/11.10",
"Mozilla/5.0 (Linux; U; Android 3.0; en-us; Xoom Build/HRI39) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13",
"Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; en) AppleWebKit/534.1+ (KHTML, like Gecko) Version/6.0.0.337 Mobile Safari/534.1+",
"Mozilla/5.0 (hp-tablet; Linux; hpwOS/3.0.0; U; en-US) AppleWebKit/534.6 (KHTML, like Gecko) wOSBrowser/233.70 Safari/534.6 TouchPad/1.0",
"Mozilla/5.0 (SymbianOS/9.4; Series60/5.0 NokiaN97-1/20.0.019; Profile/MIDP-2.1 Configuration/CLDC-1.1) AppleWebKit/525 (KHTML, like Gecko) BrowserNG/7.1.18124",
"Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; HTC; Titan)",
"UCWEB7.0.2.37/28/999",
"NOKIA5700/ UCWEB7.0.2.37/28/999",
"Openwave/ UCWEB7.0.2.37/28/999",
"Mozilla/4.0 (compatible; MSIE 6.0; ) Opera/UCWEB7.0.2.37/28/999",
# iPhone 6:
"Mozilla/6.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/8.0 Mobile/10A5376e Safari/8536.25",
]
#滚动条缓慢滚动10次
def windowscroll(num): #每次停格一秒,滚动10次。
height=0
#滚动条连续滚动10次
for i in range(num):
driver.execute_script("window.scrollTo({},{});".format(height,height+500))
height = height + 500
time.sleep(1)
headers = {'User-Agent': random.choice(user_agent)}
url = 'http://quote.eastmoney.com/center/gridlist.html#hs_a_board'
datalist=[]
field_name=[]
stock_id=[]
stock_code=[]
stock_name=[]
stock_link=[]
stock_newprice=[]
stock_change=[]
stock_rise_fall=[]
stock_turnover=[]
# 浏览器驱动绝对路径
driver = webdriver.Chrome()
# 跳转网页
driver.get(url)
#最大化
driver.maximize_window()
for i in range(269):#269 总页数-1,因为第一页已经进去了
# 滚动条缓慢滚动10次
# windowscroll(1)
time.sleep(1)
source_result = driver.page_source
# response=requests.get(url,headers=headers)
# resp=response.text
html=etree.HTML(source_result)
time.sleep(1)
#首行的名称字段
field_name.extend(html.xpath('/html/body/div[1]/div[2]/div[2]/div[5]/div/table/thead/tr/th/span/text()'))
#序号
stock_id.extend(html.xpath('/html/body/div[1]/div[2]/div[2]/div[5]/div/table/tbody/tr/td[1]/text()'))
#代码
stock_code.extend(html.xpath('/html/body/div[1]/div[2]/div[2]/div[5]/div/table/tbody/tr/td[2]/a/text()'))
#名称
stock_name.extend(html.xpath('/html/body/div[1]/div[2]/div[2]/div[5]/div/table/tbody/tr/td[3]/a/text()'))
#相关链接
stock_link.extend(html.xpath('/html/body/div[1]/div[2]/div[2]/div[5]/div/table/tbody/tr/td[4]/a[1]/@href'))
#最新价
stock_newprice.extend(html.xpath('/html/body/div[1]/div[2]/div[2]/div[5]/div/table/tbody/tr/td[5]/span/text()'))
#涨跌幅
stock_change.extend(html.xpath('/html/body/div[1]/div[2]/div[2]/div[5]/div/table/tbody/tr/td[6]/span/text()'))
#涨跌额
stock_rise_fall.extend(html.xpath('/html/body/div[1]/div[2]/div[2]/div[5]/div/table/tbody/tr/td[7]/span/text()'))
#成交量(手)
stock_turnover.extend(html.xpath('/html/body/div[1]/div[2]/div[2]/div[5]/div/table/tbody/tr/td[8]/text()'))
#翻页
if(html.xpath("/html/body/div[1]/div[2]/div[2]/div[5]/div/div[2]/div/a[2]/@class") != "next paginate_button disabled"):
driver.find_element_by_xpath('/html/body/div[1]/div[2]/div[2]/div[5]/div/div[2]/div/a[2]').click()
datalist.append(stock_id)
datalist.append(stock_code)
datalist.append(stock_name)
datalist.append(stock_link)
datalist.append(stock_newprice)
datalist.append(stock_change)
datalist.append(stock_rise_fall)
datalist.append(stock_turnover)
driver.quit()
print(datalist)
#写入excel
#创建excel表格类型文件
book = xlwt.Workbook(encoding='utf-8',style_compression=0)
#在excel表格类型文件中建立一张sheet表单 cell_overwrite_ok用于确认同一个cell单元是否可以重设值,这里赋值为True就表示可重设值。
sheet = book.add_sheet('沪深京个股',cell_overwrite_ok=True)
sheet.col(3).width = 256 * 50 # Set the column width 设置第一列列宽
#首行字段写入excel中
for i in range(len(field_name[0:8])):
sheet.write(0,i,field_name[i]) #第一个参数是行、第二个参数是列、第三个当然就是col元组值
print(len(datalist))
for i in range(len(field_name[0:8])):
for j in range(len(stock_code)):
# print(len(datalist[i]))
sheet.write(j+1,i,datalist[i][j])
savepath = 'D:/spider_file/spider_excel/stock.xls'
book.save(savepath)
最终效果如下:
我们可以看见,D列行宽通过我们的代码完成改变了,数据共爬取5380行。
后续改进:
1.目前只选取了部分字段的数据,2.0的想法是把所有字段爬取下来。
2.希望把代码完善一下,代码有些凌乱,看看能不能把部分功能封装起来。
2023-5-19 15:30
爬取网站表格数据2.0
1.将所有字段都爬取下来了。
代码如下:
import random
from selenium import webdriver
import time
from lxml import etree
import requests
import xlwt
user_agent = [
"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50",
"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50",
"Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0",
"Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; InfoPath.3; rv:11.0) like Gecko",
"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",
"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1",
"Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1",
"Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.8.131 Version/11.11",
"Opera/9.80 (Windows NT 6.1; U; en) Presto/2.8.131 Version/11.11",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Maxthon 2.0)",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; TencentTraveler 4.0)",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; The World)",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; SE 2.X MetaSr 1.0; SE 2.X MetaSr 1.0; .NET CLR 2.0.50727; SE 2.X MetaSr 1.0)",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; 360SE)",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Avant Browser)",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)",
"Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5",
"Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5",
"Mozilla/5.0 (iPad; U; CPU OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5",
"Mozilla/5.0 (Linux; U; Android 2.3.7; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1",
"MQQBrowser/26 Mozilla/5.0 (Linux; U; Android 2.3.7; zh-cn; MB200 Build/GRJ22; CyanogenMod-7) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1",
"Opera/9.80 (Android 2.3.4; Linux; Opera Mobi/build-1107180945; U; en-GB) Presto/2.8.149 Version/11.10",
"Mozilla/5.0 (Linux; U; Android 3.0; en-us; Xoom Build/HRI39) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13",
"Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; en) AppleWebKit/534.1+ (KHTML, like Gecko) Version/6.0.0.337 Mobile Safari/534.1+",
"Mozilla/5.0 (hp-tablet; Linux; hpwOS/3.0.0; U; en-US) AppleWebKit/534.6 (KHTML, like Gecko) wOSBrowser/233.70 Safari/534.6 TouchPad/1.0",
"Mozilla/5.0 (SymbianOS/9.4; Series60/5.0 NokiaN97-1/20.0.019; Profile/MIDP-2.1 Configuration/CLDC-1.1) AppleWebKit/525 (KHTML, like Gecko) BrowserNG/7.1.18124",
"Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; HTC; Titan)",
"UCWEB7.0.2.37/28/999",
"NOKIA5700/ UCWEB7.0.2.37/28/999",
"Openwave/ UCWEB7.0.2.37/28/999",
"Mozilla/4.0 (compatible; MSIE 6.0; ) Opera/UCWEB7.0.2.37/28/999",
# iPhone 6:
"Mozilla/6.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/8.0 Mobile/10A5376e Safari/8536.25",
]
#滚动条缓慢滚动10次
def windowscroll(num): #每次停格一秒,滚动10次。
height=0
#滚动条连续滚动10次
for i in range(num):
driver.execute_script("window.scrollTo({},{});".format(height,height+500))
height = height + 500
time.sleep(1)
headers = {'User-Agent': random.choice(user_agent)}
url = 'http://quote.eastmoney.com/center/gridlist.html#hs_a_board'
datalist=[]
field_name=[]
stock_id=[]
stock_code=[]
stock_name=[]
stock_link=[]
stock_newprice=[]
stock_change=[]
stock_rise_fall=[]
stock_turnover=[]
stock_amount=[]
stock_amplitude=[]
stock_max=[]
stock_min=[]
stock_today=[]
stock_yesterday=[]
stock_q_ratio=[]
stock_turnover_rate=[]
stock_PE_ratio=[]
stock_PB_ratio=[]
# 浏览器驱动绝对路径
driver = webdriver.Chrome()
# 跳转网页
driver.get(url)
#最大化
driver.maximize_window()
for i in range(269):#269 总页数-1,因为第一页已经进去了
# 滚动条缓慢滚动10次
# windowscroll(1)
time.sleep(1)
source_result = driver.page_source
# response=requests.get(url,headers=headers)
# resp=response.text
html=etree.HTML(source_result)
time.sleep(1)
#首行的名称字段
field_name.extend(html.xpath('/html/body/div[1]/div[2]/div[2]/div[5]/div/table/thead/tr/th/span/text()'))
#序号
stock_id.extend(html.xpath('/html/body/div[1]/div[2]/div[2]/div[5]/div/table/tbody/tr/td[1]/text()'))
#代码
stock_code.extend(html.xpath('/html/body/div[1]/div[2]/div[2]/div[5]/div/table/tbody/tr/td[2]/a/text()'))
#名称
stock_name.extend(html.xpath('/html/body/div[1]/div[2]/div[2]/div[5]/div/table/tbody/tr/td[3]/a/text()'))
#相关链接
stock_link.extend(html.xpath('/html/body/div[1]/div[2]/div[2]/div[5]/div/table/tbody/tr/td[4]/a[1]/@href'))
#最新价
stock_newprice.extend(html.xpath('/html/body/div[1]/div[2]/div[2]/div[5]/div/table/tbody/tr/td[5]/span/text()'))
#涨跌幅
stock_change.extend(html.xpath('/html/body/div[1]/div[2]/div[2]/div[5]/div/table/tbody/tr/td[6]/span/text()'))
#涨跌额
stock_rise_fall.extend(html.xpath('/html/body/div[1]/div[2]/div[2]/div[5]/div/table/tbody/tr/td[7]/span/text()'))
#成交量(手)
stock_turnover.extend(html.xpath('/html/body/div[1]/div[2]/div[2]/div[5]/div/table/tbody/tr/td[8]/text()'))
#成交额
stock_amount.extend(html.xpath('/html/body/div[1]/div[2]/div[2]/div[5]/div/table/tbody/tr/td[9]/text()'))
#振幅
stock_amplitude.extend(html.xpath('/html/body/div[1]/div[2]/div[2]/div[5]/div/table/tbody/tr/td[10]/text()'))
#最高
stock_max.extend(html.xpath('/html/body/div[1]/div[2]/div[2]/div[5]/div/table/tbody/tr/td[11]/span/text()'))
#最低
stock_min.extend(html.xpath('/html/body/div[1]/div[2]/div[2]/div[5]/div/table/tbody/tr/td[12]/span/text()'))
#今开
stock_today.extend(html.xpath('/html/body/div[1]/div[2]/div[2]/div[5]/div/table/tbody/tr/td[13]/span/text()'))
#昨收
stock_yesterday.extend(html.xpath('/html/body/div[1]/div[2]/div[2]/div[5]/div/table/tbody/tr/td[14]/text()'))
#量比
stock_q_ratio.extend(html.xpath('/html/body/div[1]/div[2]/div[2]/div[5]/div/table/tbody/tr/td[15]/text()'))
#换手率
stock_turnover_rate.extend(html.xpath('/html/body/div[1]/div[2]/div[2]/div[5]/div/table/tbody/tr/td[16]/text()'))
#市盈率
stock_PE_ratio.extend(html.xpath('/html/body/div[1]/div[2]/div[2]/div[5]/div/table/tbody/tr/td[17]/text()'))
#市净率
stock_PB_ratio.extend(html.xpath('/html/body/div[1]/div[2]/div[2]/div[5]/div/table/tbody/tr/td[18]/text()'))
#翻页
if(html.xpath("/html/body/div[1]/div[2]/div[2]/div[5]/div/div[2]/div/a[2]/@class") != "next paginate_button disabled"):
driver.find_element_by_xpath('/html/body/div[1]/div[2]/div[2]/div[5]/div/div[2]/div/a[2]').click()
# datalist.append(field_name)
datalist.append(stock_id)
datalist.append(stock_code)
datalist.append(stock_name)
datalist.append(stock_link)
datalist.append(stock_newprice)
datalist.append(stock_change)
datalist.append(stock_rise_fall)
datalist.append(stock_turnover)
datalist.append(stock_amount)
datalist.append(stock_amplitude)
datalist.append(stock_max)
datalist.append(stock_min)
datalist.append(stock_today)
datalist.append(stock_yesterday)
datalist.append(stock_q_ratio)
datalist.append(stock_turnover_rate)
datalist.append(stock_PE_ratio)
datalist.append(stock_PB_ratio)
driver.quit()
print(datalist)
#写入excel
#创建excel表格类型文件
book = xlwt.Workbook(encoding='utf-8',style_compression=0)
#在excel表格类型文件中建立一张sheet表单 cell_overwrite_ok用于确认同一个cell单元是否可以重设值,这里赋值为True就表示可重设值。
sheet = book.add_sheet('沪深京个股',cell_overwrite_ok=True)
sheet.col(3).width = 256 * 50 # Set the column width 设置第一列列宽
#首行字段写入excel中
for i in range(len(field_name[0:17])):
sheet.write(0,i,field_name[i]) #第一个参数是行、第二个参数是列、第三个当然就是col元组值
# print(len(datalist))
for i in range(len(field_name[0:17])):
for j in range(len(stock_id)):
# print(len(datalist[i]))
sheet.write(j+1,i,datalist[i][j])
savepath = 'D:/spider_file/spider_excel/stock.xls'
book.save(savepath)
实际结果如下:
很早之前写的代码了,自己搬运一下的