Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.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
Webcrawler:在mac上使用Python3从数组中提取字符串_Python_Regex_Dictionary_Web Crawler - Fatal编程技术网

Webcrawler:在mac上使用Python3从数组中提取字符串

Webcrawler:在mac上使用Python3从数组中提取字符串,python,regex,dictionary,web-crawler,Python,Regex,Dictionary,Web Crawler,我在编写webcrawler提取汇率时遇到问题: import requests from bs4 import BeautifulSoup from urllib.parse import urljoin import re url = "https://wechselkurse-euro.de/" r = requests.get(url) rates = [] status = r.status_code if status != 200: print("Something

我在编写webcrawler提取汇率时遇到问题:

import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin
import re


url = "https://wechselkurse-euro.de/"

r = requests.get(url)
rates = []
status = r.status_code

if status != 200:
    print("Something went wrong while parsing the website " + url)

temp = BeautifulSoup(r.text, "html.parser")
current_date = temp.select(".ecb")[0].text.strip().split(" ")[5]

#rates_array = temp.select(".kurz_kurz2.center", limit= 20).string

rates_array = temp.select(".kurz_kurz2.center", limit= 20)

#for i in rates_array:
#    rate = rates_array[i].string
#    rates.append(rate)

rates = list( map( lambda x: re.search(">\d{1}\.\d{4}",x), rates_array))

print(rates)

#rate_1EUR_to_USD =  
#rate_1EUR_to_GBP =


我尝试了几种被注释掉的方法——它们都不起作用,我也不知道为什么。尤其是.string不起作用让我感到惊讶,因为rates_数组似乎继承了bs4对象的所有不同信息,包括有一个td标记
0.5554
的信息,我只希望该字符串在标记中(因此上面示例中的值为0.5554)。这应该很容易,但没有任何效果,我做错了什么

正则表达式应该不是问题,我在regExR上测试了它

我尝试将map函数用作当前活动的函数,但无法按预期将map对象转换为列表

select().string返回一个空列表,当我尝试用for循环迭代函数的每一项时,使用正则表达式搜索保存在rates_数组中的字符串也是如此


我建议您先检查定位器。 您确定rates\u数组不是空的吗? 此外,请尝试:
rates_数组[i]。text

我建议您先检查定位器。 您确定rates\u数组不是空的吗? 此外,请尝试:
rates_array[i]。文本您的
rates_array
包含而不是字符串。因此,您必须访问它们的
text
属性才能获取值。例如:

rates=[o.text表示速率数组中的o]
现在,
费率
包含:

['0.5554','0.1758']

您的
rates\u数组
包含而不是字符串。因此,您必须访问它们的
text
属性才能获取值。例如:

rates=[o.text表示速率数组中的o]
现在,
费率
包含:

['0.5554','0.1758']

尝试
查看速率数组中的o\u:打印(键入(o),o.text)
:)尝试
在速率数组中输入o\u:print(键入(o),o.text)
:)该死的,我试的太复杂了。。。我仍然不明白为什么o.text可以工作,但是填充数组时.text或.string不能工作。但是谢谢@先生,您也可以通过
[item.text for item in temp.select(.kurz_kurz2.center,limit=20)]
访问文本属性,但您不能只将属性选择器附加到
temp.select(…)
,因为它是一个结果集,而不是单个结果项。你必须对集合中的所有项目采取行动,因此列表理解。该死,我尝试的方式太复杂了。。。我仍然不明白为什么o.text可以工作,但是填充数组时.text或.string不能工作。但是谢谢@先生,您也可以通过
[item.text for item in temp.select(.kurz_kurz2.center,limit=20)]
访问文本属性,但您不能只将属性选择器附加到
temp.select(…)
,因为它是一个结果集,而不是单个结果项。您必须对集合中的所有项目执行操作,因此list.rates\u数组不是空的,请选中该选项。也尝试通过索引进行访问。因为。文本也可以在上面工作,我想你的方式也可以。谢谢rates\u数组不为空,已检查是否为空。也尝试通过索引进行访问。因为。文本也可以在上面工作,我想你的方式也可以。谢谢