1045, "Access denied for user 'root'@'localhost' (using password: YES)"

DavinciVon 2020-02-13 12:06:18
编译环境python3.7+mysql5.7+mongo4.2。系统为win10。
在写pyhton爬虫的时候遇到了这个问题,下面贴上两个代码。第一个代码是提取的国家country,各国AS数量nums,分别对应的子链接。第二个代码就是在子页面具体爬取信息。请各位大佬帮忙看看是哪里出了问题。第一个代码执行没问题,已经将数据存入mysql。问题出现在第二个代码。

import requests
from lxml import etree

import pymysql

#con = pymysql.connect(host='127.0.0.1',user='root',passwd='123456',db='lab',charset='utf8')

con = pymysql.connect(host='localhost',user='root',passwd='youpassword',db='ASNS',charset='utf8')

headers={
"Accept": "text/javascript, text/html, application/xml, text/xml, */*",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "zh-CN,zh;q=0.9,zh-TW;q=0.8,en;q=0.7",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36",
"Host": "bgp.he.net",
"Connection":"close",
"Origin": "https://bgp.he.net",
"Referer": "https://bgp.he.net/"
}

res = requests.get('https://bgp.he.net/report/world', headers = headers)
html = etree.HTML(res.text)
names = html.xpath("//div[@id='countries']/table/tbody/tr/td/div[@class='down2 floatleft']/text()")
counts = html.xpath("//div[@id='countries']/table/tbody/tr/td[@class='alignright']/text()")
hrefs = html.xpath("//div[@id='countries']/table/tbody/tr/td/a/@href")
cursor = con.cursor()
#print(hrefs[0])
for i in range(242):
nums = counts[i].replace(',', '').replace('\t', '').replace('\n', '').replace(' ','')
href = hrefs[i]
name = names[i].replace('\t', '').replace('\n', '').replace(' ','')
cursor.execute('insert into ASs(country, nums, url) values(%s, %s, %s)', (name, nums, href))
cursor.close()
con.commit()


下面是第二个代码
import json
import np
import pymongo
import pymysql
import requests
import time
import numpy
from pymongo import MongoClient
from lxml import etree

headers={
"Accept": "text/javascript, text/html, application/xml, text/xml, */*",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "zh-CN,zh;q=0.9,zh-TW;q=0.8,en;q=0.7",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36",
"Host": "bgp.he.net",
"Connection":"close",
"Origin": "https://bgp.he.net",
"Referer": "https://bgp.he.net/cc"
}

client = pymongo.MongoClient('mongodb://localhost:27017/')

mydb = client['AS']

DAY = time.strftime("%Y_%m_%d", time.localtime(time.time()))

mycol = mydb['DATAS_' + DAY]

def get_Countries():
con = pymysql.connect(host='localhost',user='root',passwd=' youpassword',db='ASNS',charset='utf8')
cursor = con.cursor()
cursor.execute("select * from ASs")
res = cursor.fetchall()
cursor.close()
con.close()
return res

def get_ASNs():
countries = get_Countries()
for country in countries:
country_name = country[0]
asn_number_o = country[1]
asn_url = country[2]
res = requests.get('http://bgp.he.net' + asn_url, headers = headers)
html = etree.HTML(res.text)
ASNs = {}
ASN_Parts = html.xpath("//div[@id='country']//tr/td[@class='alignright']/..")
for ASN_Part in ASN_Parts:
ASN_Num = ASN_Part.xpath("./td/a/text()")[0]
Attach_Info = ASN_Part.xpath("./td/text()")
try:
ASNs[ASN_Num] = {
"Organization": Attach_Info[0],
"Adjacency_V4": Attach_Info[1],
"Route_V4": Attach_Info[2],
"Adjacency_V6": Attach_Info[3],
"Route_V6": Attach_Info[4]
}
except Exception as e:
ASNs[ASN_Num] = {
"Organization": '',
"Adjacency": Attach_Info[0],
"Route_V4": Attach_Info[1],
"Adjacency_V6": Attach_Info[2],
"Route_V6": Attach_Info[3]
}
#print(json.dumps(ASNs, indent = 4))
mycol.insert_one({
"Country": country_name,
"ASN_Number_Register": asn_number_o,
"ASN_Number_Find": len(ASN_Parts),
"ASNs": ASNs
})


if __name__ == '__main__':
get_ASNs()



下面是报错信息。

D:\PyCharm_workplace\PyCharmProfession_workplace\venv\Scripts\python.exe D:/PyCharm_workplace/query_country.py
Traceback (most recent call last):
File "D:/PyCharm_workplace/query_country.py", line 78, in <module>
get_ASNs()
File "D:/PyCharm_workplace/query_country.py", line 40, in get_ASNs
countries = get_Countries()
File "D:/PyCharm_workplace/query_country.py", line 31, in get_Countries
con = pymysql.connect(host='localhost',user='root',passwd=' youpassword',db='ASNS',charset='utf8')
File "D:\PyCharm_workplace\PyCharmProfession_workplace\venv\lib\site-packages\pymysql\__init__.py", line 94, in Connect
return Connection(*args, **kwargs)
File "D:\PyCharm_workplace\PyCharmProfession_workplace\venv\lib\site-packages\pymysql\connections.py", line 325, in __init__
self.connect()
File "D:\PyCharm_workplace\PyCharmProfession_workplace\venv\lib\site-packages\pymysql\connections.py", line 599, in connect
self._request_authentication()
File "D:\PyCharm_workplace\PyCharmProfession_workplace\venv\lib\site-packages\pymysql\connections.py", line 861, in _request_authentication
auth_packet = self._read_packet()
File "D:\PyCharm_workplace\PyCharmProfession_workplace\venv\lib\site-packages\pymysql\connections.py", line 684, in _read_packet
packet.check_error()
File "D:\PyCharm_workplace\PyCharmProfession_workplace\venv\lib\site-packages\pymysql\protocol.py", line 220, in check_error
err.raise_mysql_exception(self._data)
File "D:\PyCharm_workplace\PyCharmProfession_workplace\venv\lib\site-packages\pymysql\err.py", line 109, in raise_mysql_exception
raise errorclass(errno, errval)
pymysql.err.OperationalError: (1045, "Access denied for user 'root'@'localhost' (using password: YES)")

...全文
550 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

1,746

社区成员

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

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