python爬取网页数据并存储到mysql数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#python 3.5
from urllib.request import urlopen
from urllib.request import urlretrieve
from bs4 import BeautifulSoup
import pymysql
 
conn = pymysql.connect(host='127.0.0.1',user='root',passwd='root',db='pmdb',charset='utf8')
cur = conn.cursor()<br>
url = "http://www.mm131.com/xinggan/"
html = urlopen(url)
soup = BeautifulSoup(html,"lxml")
urllist = soup.find(class_='list-left public-box')
for i in urllist:
    jlist = soup.find("dd").find("a")["href"]
    jliststr='\''+jlist+'\''
    # print(jlist)
    cur.execute("insert into href(href) VALUES ("+jliststr+")")
    conn.commit()
cur.close()
conn.close()
print("成功!")

作者:江离子

出处:https://www.cnblogs.com/zhouwp/p/8316467.html

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   江离子  阅读(6054)  评论(0编辑  收藏  举报
编辑推荐:
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
阅读排行:
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· PPT革命!DeepSeek+Kimi=N小时工作5分钟完成?
· What?废柴, 还在本地部署DeepSeek吗?Are you kidding?
· DeepSeek企业级部署实战指南:从服务器选型到Dify私有化落地
· 程序员转型AI:行业分析
more_horiz
keyboard_arrow_up light_mode palette
选择主题
点击右上角即可分享
微信分享提示