使用python BeautifulSoup庫(kù)抓取58手機(jī)維修信息
直接上代碼:
#!/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中就會(huì)有商家的地址和電話等信息。
BeautifulSoup api 的地址為: http://www.crummy.com/software/BeautifulSoup/bs4/doc/
- Python爬取求職網(wǎng)requests庫(kù)和BeautifulSoup庫(kù)使用詳解
- Python實(shí)戰(zhàn)快速上手BeautifulSoup庫(kù)爬取專欄標(biāo)題和地址
- python爬蟲beautifulsoup庫(kù)使用操作教程全解(python爬蟲基礎(chǔ)入門)
- python BeautifulSoup庫(kù)的安裝與使用
- Python獲取基金網(wǎng)站網(wǎng)頁內(nèi)容、使用BeautifulSoup庫(kù)分析html操作示例
- python用BeautifulSoup庫(kù)簡(jiǎn)單爬蟲實(shí)例分析
- Python使用BeautifulSoup庫(kù)解析HTML基本使用教程
- Python?使用BeautifulSoup庫(kù)的方法
相關(guān)文章
Python內(nèi)置模塊Collections的使用教程詳解
collections 是 Python 的一個(gè)內(nèi)置模塊,所謂內(nèi)置模塊的意思是指 Python 內(nèi)部封裝好的模塊,無需安裝即可直接使用。本文將詳解介紹Collections的使用方式,需要的可以參考一下2022-03-03
Pytorch在訓(xùn)練時(shí)凍結(jié)某些層使其不參與訓(xùn)練問題(更新梯度)
這篇文章主要介紹了Pytorch在訓(xùn)練時(shí)凍結(jié)某些層使其不參與訓(xùn)練問題(更新梯度),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
解決python 未發(fā)現(xiàn)數(shù)據(jù)源名稱并且未指定默認(rèn)驅(qū)動(dòng)程序的問題
今天小編就為大家分享一篇解決python 未發(fā)現(xiàn)數(shù)據(jù)源名稱并且未指定默認(rèn)驅(qū)動(dòng)程序的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-12-12

