使用python BeautifulSoup庫抓取58手機維修信息
直接上代碼:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import urllib
import os,datetime,string
import sys
from bs4 import BeautifulSoup
reload(sys)
sys.setdefaultencoding('utf-8')
__BASEURL__ = 'http://bj.58.com/'
__INITURL__ = "http://bj.58.com/shoujiweixiu/"
soup = BeautifulSoup(urllib.urlopen(__INITURL__))
lvlELements = soup.html.body.find('div','selectbarTable').find('tr').find_next_sibling('tr')('a',href=True)
f = open('data1.txt','a')
for element in lvlELements[1:]:
f.write((element.get_text()+'\n\r' ))
url = __BASEURL__ + element.get('href')
print url
soup = BeautifulSoup(urllib.urlopen(url))
lv2ELements = soup.html.body.find('table','tblist').find_all('tr')
for item in lv2ELements:
addr = item.find('td','t').find('a').get_text()
phone = item.find('td','tdl').find('b','tele').get_text()
f.write('地址:'+addr +' 電話:'+ phone + '\r\n\r')
f.close()
直接執(zhí)行后,存在 data1.txt中就會有商家的地址和電話等信息。
BeautifulSoup api 的地址為: http://www.crummy.com/software/BeautifulSoup/bs4/doc/
- Python爬取求職網(wǎng)requests庫和BeautifulSoup庫使用詳解
- Python實戰(zhàn)快速上手BeautifulSoup庫爬取專欄標(biāo)題和地址
- python爬蟲beautifulsoup庫使用操作教程全解(python爬蟲基礎(chǔ)入門)
- python BeautifulSoup庫的安裝與使用
- Python獲取基金網(wǎng)站網(wǎng)頁內(nèi)容、使用BeautifulSoup庫分析html操作示例
- python用BeautifulSoup庫簡單爬蟲實例分析
- Python使用BeautifulSoup庫解析HTML基本使用教程
- Python?使用BeautifulSoup庫的方法
相關(guān)文章
tensor.squeeze函數(shù)和tensor.unsqueeze函數(shù)的使用詳解
本文主要介紹了tensor.squeeze函數(shù)和tensor.unsqueeze函數(shù)的使用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03Python實現(xiàn)破解12306圖片驗證碼的方法分析
這篇文章主要介紹了Python實現(xiàn)破解12306圖片驗證碼的方法,涉及Python圖片截取、調(diào)用百度識圖及正則截取等相關(guān)操作技巧,需要的朋友可以參考下2017-12-12Pycharm 實現(xiàn)下一個文件引用另外一個文件的方法
今天小編就為大家分享一篇Pycharm 實現(xiàn)下一個文件引用另外一個文件的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01python中關(guān)于時間和日期函數(shù)的常用計算總結(jié)(time和datatime)
python中關(guān)于時間和日期函數(shù)有time和datatime使用介紹,需要的朋友可以參考下2013-03-03運行python提示no module named sklearn的解決方法
這篇文章主要介紹了運行python提示no module named sklearn的解決方法,需要的朋友可以參考下2020-11-11