課程
/后端開發(fā)
/Python
/python遇見數(shù)據(jù)采集
MySQL版本5.7.17,Python版本3.6.0,鼓搗一下午都沒弄出來
2017-03-20
源自:python遇見數(shù)據(jù)采集 4-1
正在回答
import?io import?sys import?re import?pymysql.cursors from?urllib.request?import?urlopen from?bs4?import?BeautifulSoup?as?bs sys.stdout?=?io.TextIOWrapper(sys.stdout.buffer,?encoding='utf8') #?請求URL,并把結(jié)果用UTF-8編碼 resp?=?urlopen("https://en.wikipedia.org/wiki/Wiki").read().decode('utf-8') #?使用BeautifulSoup去解析 soup?=?bs(resp,?"html.parser") #?獲取所有以/wiki/開頭的a標(biāo)簽的href屬性 listurls?=?soup.find_all('a',?href=re.compile("^/wiki/")) #?輸出所有的詞條對應(yīng)的名稱和URL for?url?in?listurls: ????#?過濾以.jpg或.JPG結(jié)尾的URL ????if?not?re.search("\.(jpg|JPG)$",?url["href"]): ????????#?輸出URL的文字對應(yīng)的鏈接 ????????#?string只能獲取一個?get_text()獲取標(biāo)簽下所有的文字 ????????print(url.get_text(),"<---->","https://en.wikipedia.org"?+?url["href"]) ????????#?獲取數(shù)據(jù)庫鏈接 ????????connection?=?pymysql.connect(host="localhost",user="root",password="123456",db="wikiurl",charset="utf8mb4") ????????try: ????????????#?獲取會話指針 ????????????with?connection.cursor()?as?cursor: ????????????????#?創(chuàng)建sql語句 ????????????????sql?=?'INSERT?INTO?`urls`(`urlname`,`urlhref`)VALUES(%s,%s)' ????????????????#?執(zhí)行sql語句 ????????????????cursor.execute(sql,(url.get_text(),"https://en.wikipedia.org"?+?url["href"])) ????????????????#?提交 ????????????????connection.commit() ????????finally: ????????????connection.close()
Mr_Blue_001 提問者
是什么原因呢?我也出現(xiàn)了相同的問題~
蘭陵蕭蕭笙 回復(fù) Mr_Blue_001 提問者
Mr_Blue_001 提問者 回復(fù) 蘭陵蕭蕭笙
#?引入開發(fā)包 from?urllib.request?import?urlopen from?bs4?import?BeautifulSoup?as?bs import?re import?pymysql.cursors #?請求URL,并把結(jié)果用UTF-8編碼 resp?=?urlopen("https://zh.wikipedia.org/wiki/Wikipedia:%E9%A6%96%E9%A1%B5").read().decode("utf-8") #?使用BeautifulSoup去解析 soup?=?bs(resp,?"html.parser") #?獲取所有以/wiki/開頭的a標(biāo)簽的href屬性 listUrls?=?soup.findAll("a",?href=re.compile("^/wiki/")) #?輸出所有的詞條對應(yīng)的名稱和URL for?url?in?listUrls: ????#?過濾以.jpg或.JPG結(jié)尾的URL ????if?not?re.search("\.(jpg|JPG)$",?url["href"]): ????????#?輸出URL的文字對應(yīng)的鏈接 ????????#?string只能獲取一個?get_text()獲取標(biāo)簽下所有的文字 ????????print(url.get_text(),?"<---->",?"https://zh.wikipedia.org"?+?url["href"]) ????????#?獲取數(shù)據(jù)庫鏈接 ????????connection?=?pymysql.connect(host='localhost', ?????????????????????????????????????user='root', ?????????????????????????????????????password='root', ?????????????????????????????????????db='wikiurl', ?????????????????????????????????????charset='utf8mb4') ????????try: ????????????#?獲取會話指針 ????????????with?connection.cursor()?as?cursor: ????????????????#?創(chuàng)建sql語句 ????????????????sql?=?"inset?into?`urls`?(`urlname`,?`urlhref`)?values(%s,?%s)" ????????????????#?執(zhí)行sql語句 ????????????????cursor.execute(sql,?(url.get_text(),?"https://zh.wikipedia.org"?+?url["href"])) ????????????????#?提交 ????????????????connection.commit() ????????finally: ????????????connection.close()
貼代碼看看
濃烈的咖啡 回復(fù) Mr_Blue_001 提問者
Mr_Blue_001 提問者 回復(fù) 濃烈的咖啡
舉報
本教程讓你初步掌握Python進(jìn)行數(shù)據(jù)采集,創(chuàng)造屬于你的價值
1 回答beatuifulsoup沒報錯,但和預(yù)想的結(jié)果不一樣
1 回答按照老師的代碼敲的,為什么會報錯?求大神解答
1 回答輸出亂碼,報錯
4 回答執(zhí)行代碼出錯,卿幫忙找一下原因
5 回答在4-2 查詢MySQL數(shù)據(jù)那一課上按照老師的代碼出現(xiàn)錯誤!找不到解決辦法!
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2017-03-21
2017-03-29
是什么原因呢?我也出現(xiàn)了相同的問題~
2017-03-21
2017-03-20