python爬取51job中hr的邮箱
程序员文章站
2022-05-09 22:01:21
...
#encoding=utf8
import urllib2
import cookielib
import re
import lxml.html
from _ast import TryExcept
from warnings import catch_warnings
f = open('/root/Desktop/51-01.txt','a+')
def read(city):
url = 'http://www.51job.com/'+city
cj = cookielib.MozillaCookieJar()
cookie_support = urllib2.HTTPCookieProcessor(cj)
opener = urllib2.build_opener(cookie_support)
opener.addheaders = [('User-agent','Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0 Iceweasel/38.3.0')]
urllib2.install_opener(opener)
response = urllib2.urlopen(url)
http = response.read()
rex = 'http://jobs.51job.com/hot/.*?html'
value = re.findall(rex, http)
for i in value:
print i
try:
readpage(i)
except:
pass
def readpage(url):
cj = cookielib.MozillaCookieJar()
cookie_support = urllib2.HTTPCookieProcessor(cj)
opener = urllib2.build_opener(cookie_support)
opener.addheaders = [('User-agent','Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0 Iceweasel/38.3.0')]
urllib2.install_opener(opener)
html = urllib2.urlopen(url,timeout = 2).read()
doc = lxml.html.fromstring(html)
rex = r'[\w\.-][email protected](?:[A-Za-z0-9]+\.)+[A-Za-z]+'
results = doc.xpath('//div[@class="tmsg inbox"]/div[@class="con_msg"]/div[@class="in"]/p/text()')
for i in results:
xx = re.compile(rex)
for j in xx.findall(i):
print j
f.write(j+'\n')
f.flush()
if __name__ == '__main__':
city_list = ['zhangjiagang','zhanjiang','zhaoqing','zibo']
for i in city_list:
f.write(i+'\n')
f.flush()
try:
read(i)
except:
pass
f.flush()
f.close()
city_list大家自己整理一下,只能帮你们到这里了。
转载于:https://my.oschina.net/passer007/blog/675156
推荐阅读
-
python爬取51job中hr的邮箱
-
python如何爬取网页中的文字
-
Python爬取六国家(地区)最流行的股票并保存到excel的不同的工作表中
-
【爬虫学习三】 Python将爬取的数据存储到MongoDB中
-
Python 读取Excel表格中的关键字进行批量爬取
-
用Python爬取日向、樱坂成员blog中的JPG文件的url并将其下载到本地
-
Python 爬取网页中JavaScript动态添加的内容(一)
-
Python 爬取网页中JavaScript动态添加的内容(二)
-
Python中Cookies爬取网页数据的方法
-
python爬虫:使用xpath和find两种方式分别实现使用requests_html库爬取网页中的内容