- 论坛徽章:
- 0
|
抓网站的代码在这里
- def openurl(id):
- url ="""http://""" + str(id) + """.xxx.com"""
- print url
- try:
- html_date = urllib.urlopen(url).read()
- #print html_date
- except:
- print 'find a error'
- pass
- else:
- logfile='log/' + str(id) +".html"
- open(logfile,'w').write(html_date)
- error_re=re.compile(r"""(数据库暂时没有您要查找的数据)""")
- companyinfo_re=re.compile(r"""<font style="font-size:12pt">(.*?)</font>""", re.S|re.M)
- Mobile_re=re.compile(r"<td align=center>手机:</td>.*?<td align=center>(.*?)</td>", re.S|re.M)
- Fax_re=re.compile(r"<td align=center>传真:</td>.*?<td align=center>(.*?)</td>", re.S|re.M)
- clear_re= re.compile( r"(<.*?>| |\s+|\n|\t|\r|\\|/|')")
- info_re=re.compile(r"(企业类型|.....|管理体系认证):</b></td>.*?<t.*?>(.*?)</td>", re.S|re.M)
- db_user = ""
- db_pw = ""
- db = MySQLdb.connect("localhost", db_user, db_pw ,"。。。")
- c = db.cursor()
- if error_re.search(html_date):
- print "数据库暂时没有您要查找的数据"
- open('error.log','a').writelines(str(id)+"\n")
- return
- try:
-
- if info_re.finditer(html_date):
- companyinfo_group=companyinfo_re.search(html_date)
- temp_info=companyinfo_group.groups()[0]
- company_info=clear_re.sub('',temp_info)
- mobile_group=Mobile_re.search(html_date)
- mobile=clear_re.sub('',mobile_group.groups()[0])
- print mobile
- Fax_group=Fax_re.search(html_date)
- Fax=clear_re.sub('',Fax_group.groups()[0])
- print Fax
- company_dict={"":""}
- for m in info_re.finditer(html_date):
- info_cat=m.groups()[0]
- name=clear_re.sub('',m.groups()[1])
- company_dict[info_cat]=name
- print "%s %s %s " % (info_cat , "," ,name)
- insert_data="insert into company (Company_ID.....) values ('" + str(id) + "' ..... +"')"
- c.execute(insert_data)
- open('work.log','a').writelines(str(id)+"\n")
- return
- else:
- print "open url error"
- open('error.log','a').writelines(str(id)+"\n")
- return
-
- except:
- print "数据有错误"
- open('error.log','a').writelines(str(id)+"\n")
- return
-
- #print url
复制代码
[ 本帖最后由 fandatou 于 2006-12-19 23:43 编辑 ] |
|