python获取网页内容_简单的python爬虫 --获取当前网页内容

import urllib.request

import mysql.connector

from Tools.scripts.treesync import raw_input

import bs4

from bs4 import BeautifulSoup

#爬取的网页地址

response = urllib.request.urlopen('https://jn.fang.lianjia.com/loupan/lixia/nht1/#lixia')

buff = response.read()

#编码方式

html = buff.decode("utf-8")

html_doc =html

soup = BeautifulSoup(html_doc,'html.parser')

#数据库操作

conn = mysql.connector.connect(user='****', password='****', database='****')

cursor = conn.cursor()

#存放的表及字段

sql="""insert into ll_pachong(LL_NAME,LL_PRICE,LL_XH) VALUES """

number=0

try:

# 执行sql语句

print('-----------------------------------------------------------------------')

cursor.execute('select * from ll_pachong ')

values = cursor.fetchall()

print('输出结果:', values)

#获取需要的内容并存入mysql数据库

for link in soup.select('.resblock-list-wrapper'):

for ul in link.select('.resblock-desc-wrapper'):

number=number+1

nam=(ul.select('.name'))[0].get_text()

price=(ul.select('.number'))[0].get_text()

cursor.execute("insert into ll_pachong(LL_NAME,LL_PRICE,LL_XH) VALUES ('"+nam+"','"+price+"','"+str(number)+"')")

print((ul.select('.name'))[0].get_text())

print((ul.select('.number'))[0].get_text())

# 提交到数据库执行

conn.commit()

except Exception as e:

raise e

# 发生错误时回滚

conn.rollback()

cursor.close()

conn.close()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值