Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Python提取_Python_Mysql - Fatal编程技术网

用Python提取

用Python提取,python,mysql,Python,Mysql,我目前正在开发一个python程序,从股票网站中提取信息 我需要提取所有列符号-卷。在这个程序之前,我必须创建一个bash脚本,每分钟下载一次页面,持续1小时,得到60页。我已经做到了。但我不知道如何提取信息,以便将这些信息注入MySQL数据库 import libxml2 import sys import os import commands import re import sys import MySQLdb from xml.dom.minidom import parse, p

我目前正在开发一个python程序,从股票网站中提取信息

我需要提取所有列符号-卷。在这个程序之前,我必须创建一个bash脚本,每分钟下载一次页面,持续1小时,得到60页。我已经做到了。但我不知道如何提取信息,以便将这些信息注入MySQL数据库

import libxml2
import sys
import os
import commands
import re
import sys

import MySQLdb

from xml.dom.minidom import parse, parseString

# for converting dict to xml 
from cStringIO import StringIO
from xml.parsers import expat

def get_elms_for_atr_val(tag,atr,val):
   lst=[]
   elms = dom.getElementsByTagName(tag)
   # ............

   return lst

# get all text recursively to the bottom
def get_text(e):
   lst=[]
   # ............
   return lst
def extract_values(dm):
   lst = []
   l = get_elms_for_atr_val('table','class','most_actives')
   # ............
   #    get_text(e)
   # ............
   return lst
我对python非常陌生,这是最好的。下载了60个HTML页面,我所需要做的就是从我相信的1个页面中提取信息,或者至少如果我可以从1个页面开始,我可以为其他页面找出一个循环,并提取这些信息以在MYsql中使用


非常感谢任何帮助我开始学习的人

使用健壮的HTML解析器,而不是
xml
模块,因为后者将拒绝格式错误的文档,如您所指的URL。下面是一个快速解决方案:

from lxml.html import parse
import sys

def process(htmlpage):
    tree = parse(htmlpage).getroot()

    # Helper function
    xpath_to_column = lambda expr: [el.text for el in tree.xpath(expr)]

    symbol = xpath_to_column('//*[@id="idcquoteholder"]/table/tr/td[1]/a')
    price  = xpath_to_column('//*[@id="idcquoteholder"]/table/tr/td[3]')
    volume = xpath_to_column('//*[@id="idcquoteholder"]/table/tr/td[6]')

    return zip(symbol, price, volume)


def main():
    for filename in sys.argv[1:]:
        with open(filename, 'r') as page:
            print process(page)


if __name__ == '__main__':
    main()
您必须对这个示例进行详细说明,因为一些元素(如“符号”)进一步包含在
span
a
节点中,但其精神是:使用XPath查询和提取列内容。根据需要添加列

提示:使用Chrome Inspector或Firebug获取正确的XPath


编辑:将命令行上的所有文件名传递到此脚本。如果需要单独处理每个文件,则删除
main()

中的
for
循环,而不是使用我使用的网站,我使用的页面是由我必须创建的bash脚本下载的,该脚本下载页面并调用python代码。这些页面的标签如下:UsaToday-2013-05-16-00.html-UsaToday-2013-05-16-59.html