簡單的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()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM