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)
Ïà¹ØÎĵµ£º
½ñÌìѧϰÁËÒ»ÏÂPythonµÄ²Ù×÷·ûÖØÔØ£¬×ܽáÁ˼¸µã±È½ÏÉñÆæµÄ¶«¶«£º
------------------------------------------------------------------------------------------------------------
¹ØÓÚiter£º
Technically, iteration contexts work by calling the iter built-in function to try to
find an _ _iter_ _ method, whi ......
(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. ......
PythonÊÇĿǰ¹ã·ºÊ¹ÓõÄÒ»ÃŶ¯Ì¬ÓïÑÔ£¬ÀàËÆJava£¬Ô´´úÂë±ØÐëÊ×ÏÈÓɱàÒëÆ÷ת»»³É×Ö½ÚÂ루byte code£©£¬È»ºóÔÙÓɽâÊÍÆ÷À´Ö´ÐÐ×Ö½ÚÂë¡£ÓëJava²»Í¬µÄÊÇ£¬PythonµÄ±àÒëÆ÷ºÍ½âÊÍÆ÷¶¼ÊÇÒ»¸ö³ÌÐò¡£Òò´Ë£¬Ô´´úÂëÒ²¿ÉÒÔÖ±½Ó½»¸øÕâ¸ö±àÒëÆ÷£¯½âÊÍÆ÷À´Ö´ÐС£
ʹÓÃPython±àд³ÌÐò£¬Äã²»ÐèÒªÔ¤ÏÈÉùÃ÷ºÃ±äÁ¿µÄÀàÐÍ£¬ÒòΪPythonÖбäÁ¿µÄÀ ......