Python 爬虫爬取页面的信息

Python 爬虫爬取页面的信息


■网友
# -*- coding: utf-8 -*-"""Created on Sun Aug 30 13:45:13 2015@author: Hang Wu"""import pandas as pd ‘‘用来生成dataframe 比较喜欢用这个来保存信息’‘’import urllib2from bs4 import BeautifulSoup as bsurl=\u0026#39;http://www.xicidaili.com/nn/1\u0026#39;soup=bs(urllib2.urlopen(url))unit=room=pd.DataFrame(columns=) for x in xrange(len(room.columns)): room]= for y in range(x,len(unit),6)]writer=pd.ExcelWriter(\u0026#39;your directory\u0026#39;)room.to_excel(writer)writer.save()这样就可以产生一个目标xlsx 文件,如下图:Python 爬虫爬取页面的信息

至于 速度啊什么,找到那个标签就行了,一样的
■网友
一收到邀请,就马上打开电脑开始写了。抱歉,本来想沿着楼主的思路用正则表达式+urllib的,结果失败了,还是用了自己更加熟悉的requests和bs4模块。Python 爬虫爬取页面的信息
【Python 爬虫爬取页面的信息】
洛克的代码比我简洁。可以比对着看。#!/usr/bin/env python# encoding: utf-8import requestsfrom bs4 import BeautifulSoupdef getInfo(url): proxy_info = page_code = requests.get(url).text soup = BeautifulSoup(page_code) table_soup = soup.find(\u0026#39;table\u0026#39;) proxy_list = table_soup.findAll(\u0026#39;tr\u0026#39;) for tr in proxy_list: td_list = tr.findAll(\u0026#39;td\u0026#39;) ip = td_list.string port = td_list.string location = td_list.string anonymity = td_list.string proxy_type = td_list.string speed = td_list.find(\u0026#39;div\u0026#39;, {\u0026#39;class\u0026#39;: \u0026#39;bar\u0026#39;}) connect_time = td_list.find(\u0026#39;div\u0026#39;, {\u0026#39;class\u0026#39;: \u0026#39;bar\u0026#39;}) validate_time = td_list.string # strip l = for i in range( len(l) ): if l: l = l.strip() proxy_info.append(l) return proxy_infoif __name__ == \u0026#39;__main__\u0026#39;: url = \u0026#39;http://www.xici.net.co/nn/1\u0026#39; proxy_info = getInfo(url) for row in proxy_info: for s in row: print s, print
■网友
soup.find_all(string = re.compile("python"))在python2版本中无法使用吗??????
哪个大佬能看见我,给我回答一下?????
拜托了......

■网友
咋不用beautifulsoup加正则表达式


    推荐阅读