Python中PyMySQL提示AttributeError: module 'socket' has no attribute 'AF_UNIX'
代码如下:
from urllib.request import urlopen
from bs4 import BeautifulSoup
import re
import datetime
import random
import pymysql
conn = pymysql.connect(host='127.0.0.1', unix_socket='/tmp/mysql.sock',
user='root', passwd=None, db='mysql', charset='utf8')
cur = conn.cursor()
cur.execute("USE scraping")
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)
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()
编译后提示错误:
Traceback (most recent call last):
File "F:\爬虫\17-9-11.py", line 9, in <module>
user='root', passwd=None, db='mysql', charset='utf8')
File "F:\Python 3.6\lib\site-packages\pymysql\__init__.py", line 90, in Connect
return Connection(*args, **kwargs)
File "F:\Python 3.6\lib\site-packages\pymysql\connections.py", line 706, in __init__
self.connect()
File "F:\Python 3.6\lib\site-packages\pymysql\connections.py", line 903, in connect
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
AttributeError: module 'socket' has no attribute 'AF_UNIX'
from urllib.request import urlopen
from bs4 import BeautifulSoup
import re
import datetime
import random
import pymysql
conn = pymysql.connect(host='127.0.0.1', unix_socket='/tmp/mysql.sock',
user='root', passwd=None, db='mysql', charset='utf8')
cur = conn.cursor()
cur.execute("USE scraping")
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)
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()
编译后提示错误:
Traceback (most recent call last):
File "F:\爬虫\17-9-11.py", line 9, in <module>
user='root', passwd=None, db='mysql', charset='utf8')
File "F:\Python 3.6\lib\site-packages\pymysql\__init__.py", line 90, in Connect
return Connection(*args, **kwargs)
File "F:\Python 3.6\lib\site-packages\pymysql\connections.py", line 706, in __init__
self.connect()
File "F:\Python 3.6\lib\site-packages\pymysql\connections.py", line 903, in connect
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
AttributeError: module 'socket' has no attribute 'AF_UNIX'
...全文
相关推荐
PyMySQL-0.7.10.tar.gz
它使用Python标准库中的“_socket”模块,不依赖于系统级别的二进制兼容性,因此可以更容易地在不同操作系统上安装和运行。
PyMySQL-1.1.0rc1.tar.gz
PyMySQL库也支持多种连接方式,包括TCP/IP,Unix Domain Socket,以及命名管道。这对于希望使用MySQL数据库的Python开发者而言,无疑提供了极大的便利。
PyMySQL 此软件包包含一个纯 Python 编写的 MySQL 和 MariaDB 客户端库,基于 PEP 249 规范
、max_allowed_packet、ssl、unix_socket、client_flag 等数十项参数,允许开发者精细控制连接生命周期、超时策略、安全策略与协议行为。
python3爬取数据至mysql的方法
以下是一个连接MySQL的示例:```pythonimport pymysqlconnect = pymysql.connect( host='192.168.142.1', unix_socket='
python 开发的mysql proxy : angel proxy
标题中的“Python开发的MySQL Proxy:Angel Proxy”指的是一个用Python编程语言编写的数据库中间件,它的主要功能是实现MySQL数据库的读写分离。


