python3爬取數(shù)據(jù)至mysql的方法
本文實(shí)例為大家分享了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)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Python3爬蟲學(xué)習(xí)之MySQL數(shù)據(jù)庫存儲(chǔ)爬取的信息詳解
- Python爬蟲爬取全球疫情數(shù)據(jù)并存儲(chǔ)到mysql數(shù)據(jù)庫的步驟
- Python爬取騰訊疫情實(shí)時(shí)數(shù)據(jù)并存儲(chǔ)到mysql數(shù)據(jù)庫的示例代碼
- python+selenium爬取微博熱搜存入Mysql的實(shí)現(xiàn)方法
- Python如何爬取51cto數(shù)據(jù)并存入MySQL
- python 爬取古詩文存入mysql數(shù)據(jù)庫的方法
- Python爬取數(shù)據(jù)并寫入MySQL數(shù)據(jù)庫的實(shí)例
- Python3實(shí)現(xiàn)的爬蟲爬取數(shù)據(jù)并存入mysql數(shù)據(jù)庫操作示例
- python Selenium爬取內(nèi)容并存儲(chǔ)至MySQL數(shù)據(jù)庫的實(shí)現(xiàn)代碼
- Python爬取京東商品信息評(píng)論存并進(jìn)MySQL
相關(guān)文章
修改默認(rèn)的pip版本為對(duì)應(yīng)python2.7的方法
今天小編就為大家分享一篇修改默認(rèn)的pip版本為對(duì)應(yīng)python2.7的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-11-11
Python如何基于selenium實(shí)現(xiàn)自動(dòng)登錄博客園
這篇文章主要介紹了Python如何基于selenium實(shí)現(xiàn)自動(dòng)登錄博客園,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
Python接口自動(dòng)化之cookie、session應(yīng)用詳解
本文主要介紹cookie、session原理及在自動(dòng)化過程中如何利用cookie、session保持會(huì)話狀態(tài)的應(yīng)用,有需要的朋友可以參考下,希望可以有所幫助2021-08-08
Python中關(guān)于函數(shù)的具體用法范例以及介紹
函數(shù)是組織好的,可重復(fù)使用的,用來實(shí)現(xiàn)單一,或相關(guān)聯(lián)功能的代碼段。函數(shù)能提高應(yīng)用的模塊性,和代碼的重復(fù)利用率。你已經(jīng)知道Python提供了許多內(nèi)建函數(shù),比如print()。但你也可以自己創(chuàng)建函數(shù),這被叫做用戶自定義函數(shù)2021-09-09
Tensorflow實(shí)現(xiàn)將標(biāo)簽變?yōu)閛ne-hot形式
這篇文章主要介紹了Tensorflow實(shí)現(xiàn)將標(biāo)簽變?yōu)閛ne-hot形式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-05-05
Python實(shí)現(xiàn)動(dòng)態(tài)添加類的屬性或成員函數(shù)的解決方法
這篇文章主要介紹了Python實(shí)現(xiàn)動(dòng)態(tài)添加類的屬性或成員函數(shù)的解決方法,在類似插件開發(fā)的時(shí)候會(huì)比較有用,需要的朋友可以參考下2014-07-07
python用什么編輯器進(jìn)行項(xiàng)目開發(fā)
在本篇文章里小編給大家整理的是一篇關(guān)于python開發(fā)用的編輯器詳細(xì)介紹,有需要的朋友們可以參考下哎。2020-06-06

