BeautifulSoup Python抓网页小例子
# -*- coding: utf-8 -*-
import urllib2
from BeautifulSoup import BeautifulSoup, Tag
import re
page = urllib2.urlopen("http://bj.ganji.com/piao/zz_%E5%8C%97%E4%BA%AC-%E5%8D%97%E6%98%8C/20100210/")
soup = BeautifulSoup(page)
#ss = soup.findAll('a', href=re.compile(r"^/piao/100."))
ss = soup.findAll(attrs={"class":"list_piao"})
fp = open("c:\\Python25\\web.html","w")
doc = '''<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>keyunq tickets</title>
<meta http-equiv="refresh" content="5"/>
<link href="http://s1.ganjistatic1.com/css/base.__1265015655__.css" mce_href="http://s1.ganjistatic1.com/css/base.__1265015655__.css" rel="stylesheet" type="text/css" />
<link href="http://s1.ganjistatic1.com/css/train.__1264669543__.css" mce_href="http://s1.ganjistatic1.com/css/train.__1264669543__.css" rel="stylesheet" type="text/css" />
<mce:style><!--
.list_piao dt { float:left; width:40%; line-height:24px; font-size:14px; text-indent:5px;padding:5px 0;}
--></mce:style><style mce_bogus="1">.list_piao dt { float:left; width:40%; line-height:24px; font-size:14px; text-indent:5px;padding:5px 0;}</style>
</head>
<body>'''
fp.write('%s\n' % doc)
for i in ss:
i.dt['class'] = 'list_piao_time'
tmp = i.a['href']
i.a['href'] = 'http://bj.ganji.com'+tmp
phonepage = urllib2.urlopen(i.a['href'])
phonesoup = BeautifulSoup(phonepage)
phone = phonesoup.findAll(attrs={"class":"phoneNum"})
tmp = phone[0].img['src']
phone[0].img['src'] = 'http://bj.ganji.com'+tmp
tag1 = Tag(soup, "dd")
tag1['class'] = 'list_piao_mj'
i.insert(8,tag1)
相关文档:
用gcc编译了一个C++调用python的程序,这个程序在VS下是好用的,而且没有使用vs的任何库
可是到了gcc下就是无法使用
后来上网查了一下资料才知道,是因为cl与gcc的运行时库不同。
打开cmd窗口,输入python就可以看到
Python 3.0 (r30:67507, Dec 3 2008, 20:14:27) [MSC v.1500 32 bit (Intel)] on win32
Type " ......
(1).当"print os.path.dirname(__file__)"所在脚本是以完整路径被运行的, 那么将输出该脚本所在的完整路径,比如:
python d:\pythonSrc\test\test.py
&nb ......
如果python调用外部程序,需要直接抓去命令行的输出,有什么好的办法呢?
这里我们需要用到 os.popen 这个管道,然后用 read、readline或者readlines来读取命令行输出
#需要执行的命令
strCommand = 'xxxxxxxxxxxxxxxxx'
#用popen来执行命令行
oStdout = os.popen(strCommand)
#假设输出的内容只有一行
strStdout = ......
Python操作Excel方法:
(1)在sourceforge.net上有一个扩展模块叫pyXLWriter,可以方便的写Excel文件。
(2)下载win32com包装上,这个包可以调用windows的com及API函数等这类的功能。Python利用win32com操作Excel。
例子:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from win32com. ......
Ubuntu平台下的Python操作Mysql
1.安装Ubuntu,安装Msql.
2.打开终端,输入 python
import MySQLdb
con = MySQLdb.connect(db="python")
cur = con.cursor()
count = cur.execute("select * from test")
print count
data = cur.fetchall()
print data
for d in data:
print d
import os
os.system('clear') ......