本文實例講述了Python基于BeautifulSoup和requests實現的爬蟲功能。分享給大家供大家參考,具體如下:
爬取的目標網頁: http://www.qianlima.com/zb/area_305/
這是一個招投標網站,我們使用python腳本爬取紅框中的信息,包括鏈接網址、鏈接名稱、時間等三項內容。
使用到的Python庫:BeautifulSoup、requests
代碼如下:
# -*- coding:utf-8 -*- import requests from bs4 import BeautifulSoup url = 'http://www.qianlima.com/zb/area_305/' user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36' headers = { 'User-Agent' : user_agent} r = requests.get(url,headers=headers)#連接 content = r.text#獲取內容,自動轉碼unicode soup = BeautifulSoup(content,"lxml") tags1 = soup.select('div .shixian_zhaobiao') tag1 = tags1[0] tag2 = tag1.find(name = 'dl') tags2 = tag2.find_all(name = 'a') tags3 = tag2.find_all(name = 'dd') for tag in tags2: print tag.get('href') print tag.string print tag.next_element.next_element.string
運行結果如下
更多關于Python相關內容可查看本站專題:《Python Socket編程技巧總結》、《Python正則表達式用法總結》、《Python數據結構與算法教程》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設計有所幫助。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
