Python中PyMySQL提示AttributeError: module 'socket' has no attribute 'AF_UNIX'

mingyaozhao 2017-09-11 07:38:14
代码如下:
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'
...全文
808 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
humanint 2018-04-07
  • 打赏
  • 举报
回复
没仔细看书吗。passwd改成你自己的MySQL密码。有个书上没讲的,你用的windows的话,删除 unix_socket='/tmp/mysql.sock'

56,677

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧