Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python “全部查找”只保留最后的值_Python_Html_Beautifulsoup_Python Requests - Fatal编程技术网

Python “全部查找”只保留最后的值

Python “全部查找”只保留最后的值,python,html,beautifulsoup,python-requests,Python,Html,Beautifulsoup,Python Requests,我的代码有问题。它应该列出所有用户名和时间值,但只显示最后的值。 我的代码: 这是因为您在每次迭代中都会覆盖name变量,因此只会得到一个列表。解决这个问题的一种方法是维护一个姓名和日期的列表 列表法 导入请求 从bs4导入BeautifulSoup 链接https://namemc.com/minecraft-names?sort=asc&length_op=&length=3&lang=&searches=500' 标题={ “用户代理”:“Mozilla/5.0(Windows NT 6.

我的代码有问题。它应该列出所有用户名和时间值,但只显示最后的值。 我的代码:


这是因为您在每次迭代中都会覆盖name变量,因此只会得到一个列表。解决这个问题的一种方法是维护一个姓名和日期的列表

列表法
导入请求
从bs4导入BeautifulSoup
链接https://namemc.com/minecraft-names?sort=asc&length_op=&length=3&lang=&searches=500'
标题={
“用户代理”:“Mozilla/5.0(Windows NT 6.1)AppleWebKit/537.36(KHTML,如Gecko)Chrome/89.0.4389.90 Safari/537.36”
}
html\u text=requests.get(链接,标题=标题)
soup=BeautifulSoup(html\u text.text,'lxml')
名称=[]
_日期=[]
users=soup.find_all('div',class_uuu='col lg order-lg-1 text nowrap text省略号')
对于用户中的用户:
names.append(user.find('a').text)
日期=汤。查找所有('div',class='col-12 col-lg-5 order-lg-2 text lg center')
以日期表示的时间:
_dates.append(time.find('time'))
如果您只想打印名称,一种更节省内存的方法是在移动中打印它们

字典法
这应该可以做到。确保您的
BeautifulSoup
版本等于或高于
4.7.0
,以便脚本获取我在脚本中使用的所需内容
:has()
伪选择器。是你应该找到更多信息的地方

import json
import requests
from bs4 import BeautifulSoup

link = 'https://namemc.com/minecraft-names?sort=asc&length_op=&length=3&lang=&searches=500'

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36'
}

data = []

html_text = requests.get(link,headers=headers)
soup = BeautifulSoup(html_text.text,'lxml')
for item in soup.select(".card-body > .no-gutters:has(time[datetime])"):
    d = {}
    d['Title'] = item.select_one("a[href^='/name/']").get_text(strip=True)
    d['Date'] = item.select_one("time[datetime]").get_text(strip=True)
    data.append(d)

print(json.dumps(data,indent=4))
输出(截断):


我建议修复缩进并将那些
print
语句移动到相应的
for循环
。或者学习如何打印迭代结果的教程。非常感谢!开始编程真的很难,我真的很感谢你的帮助。不用担心,伙计!如果你不介意的话,我还有一个问题。在这种情况下,根据结果创建词典的最佳方法是什么?从名称到日期的词典?是的。我想把这些结果包括进去。非常感谢。
import requests
from bs4 import BeautifulSoup

link = 'https://namemc.com/minecraft-names?sort=asc&length_op=&length=3&lang=&searches=500'

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36'
}

html_text = requests.get(link,headers=headers)
soup = BeautifulSoup(html_text.text,'lxml')

name_to_date = {}

users = soup.find_all('div', class_='col col-lg order-lg-1 text-nowrap text-ellipsis')
dates = soup.find_all('div', class_='col-12 col-lg-5 order-lg-2 text-lg-center')

for user, time in zip(users, dates):
    name_to_date[user.find('a').text] =time.find('time')

import json
import requests
from bs4 import BeautifulSoup

link = 'https://namemc.com/minecraft-names?sort=asc&length_op=&length=3&lang=&searches=500'

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36'
}

data = []

html_text = requests.get(link,headers=headers)
soup = BeautifulSoup(html_text.text,'lxml')
for item in soup.select(".card-body > .no-gutters:has(time[datetime])"):
    d = {}
    d['Title'] = item.select_one("a[href^='/name/']").get_text(strip=True)
    d['Date'] = item.select_one("time[datetime]").get_text(strip=True)
    data.append(d)

print(json.dumps(data,indent=4))
[
    {
        "Title": "Commander",
        "Date": "2021-03-19T13:10:40.000Z"
    },
    {
        "Title": "Tigre",
        "Date": "2021-03-19T14:06:17.000Z"
    },
    {
        "Title": "Clear",
        "Date": "2021-03-22T17:06:03.000Z"
    },
    {
        "Title": "cuddles",
        "Date": "2021-03-22T17:30:42.000Z"
    },