最近中文字幕高清中文字幕无,亚洲欧美高清一区二区三区,一本色道无码道dvd在线观看 ,一个人看的www免费高清中文字幕

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

腳本可以執(zhí)行成功,數(shù)據(jù)庫只有一條數(shù)據(jù),這是為什么?

http://img1.sycdn.imooc.com//58e8af51000181dc04800054.jpg

腳本可以執(zhí)行成功,數(shù)據(jù)庫只有一條數(shù)據(jù),這是為什么?

正在回答

9 回答

#我剛剛測(cè)試了自己的代碼,發(fā)現(xiàn)完全沒有問題
#首先,這樣,你把下面這段代碼完全復(fù)制到你的運(yùn)行環(huán)境進(jìn)行測(cè)試
from?urllib.request?import?urlopen
from?bs4?import?BeautifulSoup
import?re
import?pymysql.cursors
resp?=?urlopen("https://en.wikipedia.org/wiki/Main_Page").read().decode("utf-8")
soup?=?BeautifulSoup(resp,"html.parser")
listUrls?=?soup.findAll("a",href=re.compile(r"^/wiki/"))
for?url?in?listUrls:
???if?not?re.search("\.(jpg|JPG)$",?url["href"]):
????print(url.get_text(),"<---->","https://en.wikipedia.org"+url["href"])
????#這里要縮進(jìn),不然后面面取得URL的值就是for遍歷的卒子后一個(gè)值,才會(huì)出現(xiàn)在打印過程中沒有問題,
????#但是插入數(shù)據(jù)庫出現(xiàn)問題,
????#不過我覺得在遍歷過程的外面連接數(shù)據(jù)庫可能好點(diǎn),我覺得每一次遍歷都會(huì)連接數(shù)據(jù)庫的代價(jià)太高了
????connection=pymysql.connect(
???????host='localhost',
???????user='root',
???????password='lqmysql',
???????db='wikiurl',
???????charset='utf8mb4'
????)
????try:
?????with?connection.cursor()?as?cursor:
?????????sql="insert?into?`urls`(`urlhref`,`urlname`)values(%s,%s)"
?????????cursor.execute(sql,(url.get_text(),"https://en.wikipedia.org"+url["href"]))
?????????connection.commit()
????finally:
???????connection.close()
#查看自己的運(yùn)行結(jié)果,應(yīng)該沒有什么問題,我的就是這樣的
#
#
#然后,你復(fù)制下面這段代碼去測(cè)試一下
from?urllib.request?import?urlopen
from?bs4?import?BeautifulSoup
import?re
import?pymysql.cursors
resp?=?urlopen("https://en.wikipedia.org/wiki/Main_Page").read().decode("utf-8")
soup?=?BeautifulSoup(resp,"html.parser")
listUrls?=?soup.findAll("a",href=re.compile(r"^/wiki/"))
for?url?in?listUrls:
????if?not?re.search("\.(jpg|JPG)$",?url["href"]):
?????print(url.get_text(),"<---->","https://en.wikipedia.org"+url["href"])
connection=pymysql.connect(
????host='localhost',
????user='root',
????password='lqmysql',
????db='wikiurl',
????charset='utf8mb4'
)
try:
??with?connection.cursor()?as?cursor:
??????sql="insert?into?`urls`(`urlhref`,`urlname`)values(%s,%s)"
??????cursor.execute(sql,(url.get_text(),"https://en.wikipedia.org"+url["href"]))
??????connection.commit()
finally:
????connection.close()
#這一次,應(yīng)該只有一條語句插入了
#每次運(yùn)行完,可視化數(shù)據(jù)庫軟件要記得刷新一下


0 回復(fù) 有任何疑惑可以回復(fù)我~
#1

慕粉3745191 提問者

解決了
2017-04-09 回復(fù) 有任何疑惑可以回復(fù)我~

果然是格式問題,前車之鑒.....

0 回復(fù) 有任何疑惑可以回復(fù)我~

我的天,我也遇到了這個(gè)問題,,,,,好氣

0 回復(fù) 有任何疑惑可以回復(fù)我~

解決了……

0 回復(fù) 有任何疑惑可以回復(fù)我~
#1

追逐奔跑

如果是我猜對(duì)了問題所在,希望你采納哦~~~
2017-04-09 回復(fù) 有任何疑惑可以回復(fù)我~

http://img1.sycdn.imooc.com//58e9dd7e0001a73e04680138.jpg

看上圖

0 回復(fù) 有任何疑惑可以回復(fù)我~
from?urllib.request?import?urlopen
from?bs4?import?BeautifulSoup
import?re
import?pymysql.cursors
resp?=?urlopen("https://en.wikipedia.org/wiki/Main_Page").read().decode("utf-8")
soup?=?BeautifulSoup(resp,"html.parser")
listUrls?=?soup.findAll("a",href=re.compile("^/wiki/"))
for?url?in?listUrls:
???if?not?re.search("\.(jgp||JPG)$",?url["href"]):
????print(url.get_text(),"<---->","https://en.wikipedia.org"+url["href"])
????#這里要縮進(jìn),不然后面面取得URL的值就是for遍歷的卒子后一個(gè)值,才會(huì)出現(xiàn)在打印過程中沒有問題,
????#但是插入數(shù)據(jù)庫出現(xiàn)問題,
????#不過我覺得在遍歷過程的外面連接數(shù)據(jù)庫可能好點(diǎn),我覺得每一次遍歷都會(huì)連接數(shù)據(jù)庫的代價(jià)太高了
????connection=pymysql.connect(
???????host='localhost',
???????user='root',
???????password='lqmysql',
???????db='wikiurl',
???????charset='utf8mb4'
????)
????try:
?????with?connection.cursor()?as?cursor:
?????????sql="insert?into?`urls`(`urlhref`,`urlname`)values(%s,%s)"
?????????cursor.execute(sql,(url.get_text(),"https://en.wikipedia.org"+url["href"]))
?????????connection.commit()
????finally:
???????connection.close()


0 回復(fù) 有任何疑惑可以回復(fù)我~
#1

慕粉3745191 提問者

還是一樣,,,我排查了,運(yùn)行一次腳本,數(shù)據(jù)庫增加一條。插入的是最后的一條數(shù)據(jù)。
2017-04-09 回復(fù) 有任何疑惑可以回復(fù)我~

from urllib.request import urlopen
from bs4 import BeautifulSoup
import re
import pymysql.cursors
resp = urlopen("https://en.wikipedia.org/wiki/Main_Page").read().decode("utf-8")
soup = BeautifulSoup(resp,"html.parser")
listUrls = soup.findAll("a",href=re.compile("^/wiki/"))
for url in listUrls:
? ?if not re.search("\.(jgp||JPG)$", url["href"]):
? ? print(url.get_text(),"<---->","https://en.wikipedia.org"+url["href"])
connection=pymysql.connect(
? ?host='localhost',
? ?user='root',
? ?password='lqmysql',
? ?db='wikiurl',
? ?charset='utf8mb4'
)
try:
?with connection.cursor() as cursor:
? ? ?sql="insert into `urls`(`urlhref`,`urlname`)values(%s,%s)"
? ? ?cursor.execute(sql,(url.get_text(),"https://en.wikipedia.org"+url["href"]))
? ? ?connection.commit()
finally:
? ?connection.close()

0 回復(fù) 有任何疑惑可以回復(fù)我~
#1

追逐奔跑

如果不是慕課網(wǎng)回復(fù)過程中把你的代碼進(jìn)行的縮進(jìn),那么就是格式出了問題,Python是一個(gè)嚴(yán)格縮進(jìn)的語言,理應(yīng)從connection一直到最后一行,向后縮進(jìn)一次,或者說是向后四個(gè)空格
2017-04-09 回復(fù) 有任何疑惑可以回復(fù)我~
#2

慕粉3745191 提問者 回復(fù) 追逐奔跑

這個(gè)要怎么排查?
2017-04-09 回復(fù) 有任何疑惑可以回復(fù)我~

額,帥哥,總共不到100行代碼,你完全可以全部粘出來的,單獨(dú)這一部分應(yīng)該是沒有問題的。


0 回復(fù) 有任何疑惑可以回復(fù)我~
#1

慕粉3745191 提問者

我運(yùn)行一次腳本,數(shù)據(jù)庫里插入一條數(shù)據(jù)
2017-04-09 回復(fù) 有任何疑惑可以回復(fù)我~

是不是數(shù)據(jù)庫操作成了update?明顯是沒有Insert吧

0 回復(fù) 有任何疑惑可以回復(fù)我~
#1

慕粉3745191 提問者

執(zhí)行sql是insert
2017-04-09 回復(fù) 有任何疑惑可以回復(fù)我~
#2

追逐奔跑 回復(fù) 慕粉3745191 提問者

不介意把你寫的代碼粘出來吧?
2017-04-09 回復(fù) 有任何疑惑可以回復(fù)我~
#3

慕粉3745191 提問者 回復(fù) 追逐奔跑

try: with connection.cursor() as cursor: sql="insert into `urls`(`urlhref`,`urlname`)values(%s,%s)" cursor.execute(sql,(url.get_text(),"https://en.wikipedia.org"+url["href"])) connection.commit() finally: connection.close()
2017-04-09 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消

腳本可以執(zhí)行成功,數(shù)據(jù)庫只有一條數(shù)據(jù),這是為什么?

我要回答 關(guān)注問題
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)