python网络爬虫入门(三)

python网络爬虫练习关于房屋信息爬取的例子模块:

#-*- coding:utf-8 -*-

import requests
from bs4 import BeautifulSoup
resp = requests.get('https://wuzhong.anjuke.com/sale/litong/?from=SearchBar')
soup = BeautifulSoup(resp.text, 'lxml')

# find_all()方法,
# 注意class是Python关键词,后面要加下划线_:
alldiv = soup.find_all('div', class_='house-title')
for a in alldiv:
    names = a.find('a')['title']
    print(names)

allp = soup.find_all('div', class_='pro-price')
for p in allp:
    authors1 = p.select('span')[0].get_text()#选择p节点的第一、二个span元素,并获取span中的文字
    authors2 = p.select('span')[1].get_text()
    print(authors1)
    print(authors2)


你可能感兴趣的:(爬虫,Python,Python,爬虫)