一不小心玩了58的網(wǎng)站,|×-×|
# /bin/bash
抓取網(wǎng)頁文件,將下面url源碼保存到當前路徑下58.html文件中
curl -o 58.html http://m.58.com/zz/zufang/?58ihm=m_house_index_zufang&58cid=342&PGTID=0d200001-0015-6158-e721-99a4cce37f6f&ClickID=2
獲取指定部分的網(wǎng)頁。通過sed命令取出
<ul>``</ul>部分代碼,存放到temp文件中
cat 58.html | sed -n '/<ul class=\"list-info\"/, /<\/ul> /{//!p}' > 58.temp
截取其中含有“<a href” 字符串的行,
grep "<a href" 58.temp >58.temp1
通過正則取出url,存放到href_file中
grep -oP "(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?" 58.temp1 > href_file
那么現(xiàn)在所有工作信息的url就已經(jīng)抓取出來了,下一步好玩的,等我在做!
再更新一個版本的抓取網(wǎng)頁
看腳本:
# /bin/bash
$keyword;
echo 抓取網(wǎng)頁!
rm -f href_file
rm -f href_file.temp
rm -f 5858.temp
rm -f 5858.html
#在58兼職分類下,搜索關(guān)鍵字,關(guān)鍵字是傳入的漢字的轉(zhuǎn)譯字符
curl -o 58.html -A "Mozilla/5.0 (Linux; U; Android 4.4.4; Nexus 5 Build/KTU84P) AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30" http://m.58.com/zz/jianzhi/?key=$1
echo 截取所有的超鏈接,通過grep匹配正則表達式
cat 58.html | grep -oP "(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?" | uniq > href_file
echo 抓取符合規(guī)范的url
cat href_file | grep -o "http://.*.shtml.*" >> href_file.temp
cat href_file | grep -o "http://jump.*" >> href_file.temp
cat href_file | grep -o "http://.*.shtml" >> href_file.temp
echo 遍歷url
cat href_file.temp | while read line
do
echo "url為" $line
curl -o 5858.html $line
#輸出公司名稱
cat 5858.html | grep -o "m_detail_job_parttime_qyml.*.</a>" | grep -o ">.*.<" | grep -o "\w*" | uniq >> 5858.temp
#輸出電話號碼
cat 5858.html | grep -o "phoneno=\".*.\""|grep -o "\".*ton"|grep -oP '0\d{3}-\d{7}|1[3578]\d{9}' | uniq >> 5858.temp
done
cat 5858.temp
抓取出來的頁面,會出現(xiàn)公司名稱對應(yīng)沒有電話號,是因為這家公司隱藏了他們的電話,所有無法通過標簽獲取到電話號。能抓取出來的信息,都必須滿足上文中的匹配