pytyhon抓取网页内容get post方法
2010-08-21 16:48
417 查看
用我自己的项目做的,就是在百姓网上找的信息,然后通过post传到我自己的网站上
#!/usr/bin/python
#-*-coding:utf-8-*-
import urlparse
import sys, urllib
import re
import urllib2
urlre = re.compile(r"href=[/"']?([^ >/"']+)")
#匹配链接
ur='http://changsha.baixing.com/diannao/'
con = urllib.urlopen(ur)
result =[]
for eachline in con:
if urlre.findall(eachline):
temp = urlre.findall(eachline)
for x in temp:
#如果为站内链接,前面加上url,而且是以a开头的url,因为我想得到的是百姓<a target="_blank" href="a64095497.html">之类的网站,
if not x.startswith("http:") and x.startswith('a'):
x = urlparse.urljoin(ur,x)
result.append(x)
leng=len(result)
print leng
for url in result:
wp = urllib.urlopen(url)
#打开连接
content = wp.read()
#获取页面内容
titlere = re.compile(r"<title>(.*?)</title>",re.I)
#匹配网页源代码中的title
title = titlere.search(content).group(1).decode('UTF-8').encode('GBK')
title=title.decode('mbcs')
#因为python的字符处理,得到的将是乱码,这是将得到的中文转换成unicode
con = re.compile(r"<p>([/s/S]*)</p>",re.I)
#匹配网页代码中的<p>中的字段
cont = con.search(content).group(1).decode('UTF-8').encode('GBK')
#将得到的字符转换成unicode
print cont
s=cont.split('<br />')
#将得到的内容通过split分成一个list,具体的看网页源代码
s[0]=s[0].decode('mbcs').replace('发布时间:','')
#将字符转换,然后进行替换
print s[0]
s[1]=s[1].decode('mbcs').replace('所在地:','')
print s[1]
#判断汉字!这很重要,网上根本查不到处理的方法
if '具体' in s[2].decode('mbcs'):
s[1]=s[1]+''+s[2].decode('mbcs').replace('具体地点:','')
s[2]=s[3].decode('mbcs').replace('价格:','')
s[3]=s[4]
else:
s[2]=s[2].decode('mbcs').replace('价格:','')
t=''
for i in s[3:-1]:
i=i.decode('mbcs')+'</br>'
t+=i
if '百姓网' in s[-1].decode('mbcs'):
s[-1]=s[-1].decode('mbcs').replace('百姓网','聚信网')
print s[-1]
t+=s[-1]
print s[0]
url = 'http://www.jixincs.info/addInfo'
#打开要输入的网站
values = ({'category':'1',
'school':'',
'subcategory':'5001',
'title':title,
'sumbit':'',
'price':s[2],
'phone':'',
'content':t,
'address':s[1],
'file':''})
#将Post的值通过字典进行传递
data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page = response.read()
#网页抓取的关键点就是汉字的字符转换问题,还有就是正则表达式的匹配问题
#!/usr/bin/python
#-*-coding:utf-8-*-
import urlparse
import sys, urllib
import re
import urllib2
urlre = re.compile(r"href=[/"']?([^ >/"']+)")
#匹配链接
ur='http://changsha.baixing.com/diannao/'
con = urllib.urlopen(ur)
result =[]
for eachline in con:
if urlre.findall(eachline):
temp = urlre.findall(eachline)
for x in temp:
#如果为站内链接,前面加上url,而且是以a开头的url,因为我想得到的是百姓<a target="_blank" href="a64095497.html">之类的网站,
if not x.startswith("http:") and x.startswith('a'):
x = urlparse.urljoin(ur,x)
result.append(x)
leng=len(result)
print leng
for url in result:
wp = urllib.urlopen(url)
#打开连接
content = wp.read()
#获取页面内容
titlere = re.compile(r"<title>(.*?)</title>",re.I)
#匹配网页源代码中的title
title = titlere.search(content).group(1).decode('UTF-8').encode('GBK')
title=title.decode('mbcs')
#因为python的字符处理,得到的将是乱码,这是将得到的中文转换成unicode
con = re.compile(r"<p>([/s/S]*)</p>",re.I)
#匹配网页代码中的<p>中的字段
cont = con.search(content).group(1).decode('UTF-8').encode('GBK')
#将得到的字符转换成unicode
print cont
s=cont.split('<br />')
#将得到的内容通过split分成一个list,具体的看网页源代码
s[0]=s[0].decode('mbcs').replace('发布时间:','')
#将字符转换,然后进行替换
print s[0]
s[1]=s[1].decode('mbcs').replace('所在地:','')
print s[1]
#判断汉字!这很重要,网上根本查不到处理的方法
if '具体' in s[2].decode('mbcs'):
s[1]=s[1]+''+s[2].decode('mbcs').replace('具体地点:','')
s[2]=s[3].decode('mbcs').replace('价格:','')
s[3]=s[4]
else:
s[2]=s[2].decode('mbcs').replace('价格:','')
t=''
for i in s[3:-1]:
i=i.decode('mbcs')+'</br>'
t+=i
if '百姓网' in s[-1].decode('mbcs'):
s[-1]=s[-1].decode('mbcs').replace('百姓网','聚信网')
print s[-1]
t+=s[-1]
print s[0]
url = 'http://www.jixincs.info/addInfo'
#打开要输入的网站
values = ({'category':'1',
'school':'',
'subcategory':'5001',
'title':title,
'sumbit':'',
'price':s[2],
'phone':'',
'content':t,
'address':s[1],
'file':''})
#将Post的值通过字典进行传递
data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page = response.read()
#网页抓取的关键点就是汉字的字符转换问题,还有就是正则表达式的匹配问题
相关文章推荐
- Spider网络爬虫学习——通过Get、Post方法直接抓取网页内容
- php使用fsockopen函数发送post,get请求获取网页内容的方法
- php使用fsockopen函数发送post,get请求获取网页内容的方法
- 谷歌在GET中适当加入POST请求抓取网页内容
- 使用nodejs发起get或post请求抓取网页内容,支持中文编码
- php发送Http请求,抓取网页数据方法(cURL,file_get_contents,snoopy)
- ASP.NET抓取网页内容的实现方法
- 转载自android 开发--抓取网页解析网页内容的若干方法(网络爬虫)(正则表达式)
- file_get_contents获取不到网页内容的解决方法
- ASP.NET 抓取网页内容-Post 数据
- Python实现周期性抓取网页内容的方法
- PHP的CURL方法curl_setopt()函数案例介绍(抓取网页,POST数据)
- PHP抓取网页内容的方法
- 定义一个方法get_page(url),url参数是需要获取网页内容的网址,返回网页的内容
- PHP的CURL方法curl_setopt()函数案例介绍(抓取网页,POST数据)
- php发送get、post请求的几种方法 以及获取内容
- php用file_get_contents方法抓取网页数据出现乱码的解决方法
- C# 抓取网页内容的方法
- 用php抓取网页内容方法总结