1 Star 1 Fork 0

dengqihua/python_beautifulsoup

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
demo_5_03.py 1.02 KB
一键复制 编辑 原始数据 按行查看 历史
邓启华 提交于 2017-06-30 17:46 . 8.1
from urllib.request import urlopen
from bs4 import BeautifulSoup
import re, datetime, random, pymysql
conn = pymysql.connect('112.74.124.220', 'dengqihua', 'dengqihua520', 'phpdoc', charset='utf8')
cur = conn.cursor()
random.seed(datetime.datetime.now())
def store(title, content):
cur.execute("INSERT INTO pages (title, content) VALUES (\"%s\",\"%s\")", (title, content))
cur.connection.commit()
def getLinks(articleUrl):
html = urlopen('http://en.wikipedia.org' + articleUrl)
bsObj = BeautifulSoup(html, 'html.parser')
title = bsObj.find('h1').get_text()
content = bsObj.find('div', {'id': 'mw-content-text'}).find('p').get_text()
store(title, content)
return bsObj.find('div', {'id': 'bodyContent'}).findAll('a', href=re.compile("^(/wiki/)((?!:).)*$"))
links = getLinks('/wiki/Kevin_Bacon')
try:
while len(links) > 0:
newArticle = links[random.randint(0, len(links) - 1)].attrs['href']
print(newArticle)
links = getLinks(newArticle)
finally:
cur.close()
conn.close()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/cppwork/python_beautifulsoup.git
git@gitee.com:cppwork/python_beautifulsoup.git
cppwork
python_beautifulsoup
python_beautifulsoup
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385