python3爬取數(shù)據(jù)至mysql的方法
更新時間:2018年06月26日 08:38:49 作者:lcjasas
這篇文章主要為大家詳細介紹了python3爬取數(shù)據(jù)至mysql的方法
,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了python3爬取數(shù)據(jù)至mysql的具體代碼,供大家參考,具體內(nèi)容如下
直接貼代碼
#!/usr/local/bin/python3.5 # -*- coding:UTF-8 -*- from urllib.request import urlopen from bs4 import BeautifulSoup import re import datetime import random import pymysql connect = pymysql.connect(host='192.168.10.142', unix_socket='/tmp/mysql.sock', user='root', passwd='1234', db='scraping', charset='utf8') cursor = connect.cursor() cursor.execute('USE scraping') random.seed(datetime.datetime.now()) def store(title, content): execute = cursor.execute("select * from pages WHERE `title` = %s", title) if execute <= 0: cursor.execute("insert into pages(`title`, `content`) VALUES(%s, %s)", (title, content)) cursor.connection.commit() else: print('This content is already exist.') def get_links(acticle_url): html = urlopen('http://en.wikipedia.org' + acticle_url) soup = BeautifulSoup(html, 'html.parser') title = soup.h1.get_text() content = soup.find('div', {'id': 'mw-content-text'}).find('p').get_text() store(title, content) return soup.find('div', {'id': 'bodyContent'}).findAll('a', href=re.compile("^(/wiki/)(.)*$")) links = get_links('') try: while len(links) > 0: newActicle = links[random.randint(0, len(links) - 1)].attrs['href'] links = get_links(newActicle) print(links) finally: cursor.close() connect.close()
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Python3爬蟲學習之MySQL數(shù)據(jù)庫存儲爬取的信息詳解
- Python爬蟲爬取全球疫情數(shù)據(jù)并存儲到mysql數(shù)據(jù)庫的步驟
- Python爬取騰訊疫情實時數(shù)據(jù)并存儲到mysql數(shù)據(jù)庫的示例代碼
- python+selenium爬取微博熱搜存入Mysql的實現(xiàn)方法
- Python如何爬取51cto數(shù)據(jù)并存入MySQL
- python 爬取古詩文存入mysql數(shù)據(jù)庫的方法
- Python爬取數(shù)據(jù)并寫入MySQL數(shù)據(jù)庫的實例
- Python3實現(xiàn)的爬蟲爬取數(shù)據(jù)并存入mysql數(shù)據(jù)庫操作示例
- python Selenium爬取內(nèi)容并存儲至MySQL數(shù)據(jù)庫的實現(xiàn)代碼
- Python爬取京東商品信息評論存并進MySQL
相關文章
Virtualenv 搭建 Py項目運行環(huán)境的教程詳解
這篇文章主要介紹了Virtualenv 搭建 Py項目運行環(huán)境的詳細教程,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06python爬蟲學習筆記之Beautifulsoup模塊用法詳解
這篇文章主要介紹了python爬蟲學習筆記之Beautifulsoup模塊用法,結合實例形式詳細分析了python爬蟲Beautifulsoup模塊基本功能、原理、用法及操作注意事項,需要的朋友可以參考下2020-04-04python操作數(shù)據(jù)庫獲取結果之fetchone和fetchall的區(qū)別說明
這篇文章主要介紹了python操作數(shù)據(jù)庫獲取結果之fetchone和fetchall的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04Python IDE環(huán)境之 新版Pycharm安裝詳細教程
這篇文章主要介紹了Python IDE環(huán)境之 新版Pycharm安裝教程,本文教程給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03Python使用unicodedata實現(xiàn)字符串標準化
這篇文章主要來和大家聊一聊 Python 的一個內(nèi)置模塊:unicodedata,它是專門用來處理 unicode 字符串的,下面就一起來看看它的用法吧2023-06-06