爬取安居客

爬取安居客

coding=utf-8
import re
import urllib.request
from bs4 import BeautifulSoup
import pymysql
db = pymysql.connect("localhost","root","zgx675050748","RUNOOB",charset='utf8')
cursor = db.cursor()
cursor.execute("DROP TABLE IF EXISTS 安居客")
sql = """CREATE TABLE 安居客 (信息 varchar(1000))"""
cursor.execute(sql)
try:
    for page in range(1,5):#爬取1-4页内容
        i=0
        url = 'https://beijing.anjuke.com/tycoon/p'+str(page)+'/#'
        user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
        headers = {'User-Agent': user_agent}
        request = urllib.request.Request(url, headers=headers)
        response = urllib.request.urlopen(request)
        content = response.read().decode('utf-8')
        soup = BeautifulSoup(content, 'html.parser')
        tag_div = soup.find_all('div',{'class':'jjr-info'})

        for a in tag_div:#爬取每个人的信息
            text = a.text
            text=text.replace(' ','')
            text=text.replace('\n',' ')
            sql = """INSERT INTO 安居客(信息)VALUES ("%s")""" % (text)
            cursor.execute(sql)
            print(sql)
            db.commit()

except urllib.request.URLError as e:
    if hasattr(e, "code"):
        print(e.code)
    if hasattr(e, "reason"):
        print(e.reason)
db.close()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值