博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

python爬虫入门--抓取wiki词条

Posted on 2017-12-11 22:11  sunshine_blog  阅读(1651)  评论(0编辑  收藏  举报
from bs4 import BeautifulSoup
import re 
from urllib import request
req = request.urlopen("https://en.m.wikipedia.org/wiki/Main_Page").read().decode("utf-8");
soup = BeautifulSoup(req,"html.parser");
for tag in soup.find_all("a",href=re.compile('^/wiki/')):
    if not re.search("\.(jpg|JPG)$",tag["href"]):
        print(tag.get_text(),"<--->","http://en.m.wikipedia.org"+tag["href"]);