我编写了一个网络抓取工具,它将货币兑换价值作为嵌套列表返回,我正在尝试编写一部分代码,通过此列表搜索给定名称并提取与之关联的货币值数据。
from urllib.request import urlopen
def find_element(line, s_pattern, e_pattern, position=0):
shift = len(s_pattern)
start = line.find(s_pattern, position) + shift
position = start
end = line.find(e_pattern, position)
return (line[start:end], position)
def fetch(url):
html = urlopen(url)
records = []
i = 0
for line in html.readlines():
line = line.decode()
if "<tr><td>" not in line:
continue # skip if line don't contain rows
if "Currency" in line:
continue # skip header
start = "<tr><td>"
end = "</td>"
element, start_pos = find_element(line, start, end)
records.append([element])
start = "<td>"
values = []
for x in range(2):
element, start_pos = find_element(line, start, end, start_pos)
values.append(element)
records[i].append(values)
i = i + 1
return(records)
def findCurrencyValue(records, currency_name):
l = [[(records)]]
d = dict(l)
d[currency_name]
return(d)
def main():
url = "https://www.cs.purdue.edu/homes/jind/exchangerate.html"
records = fetch(url)
findCurrencyValue(records, "Argentine Peso")
print(findCurrencyValue)
print("currency exchange information is\n", records)
main()
但我收到了错误
ValueError: dictionary update sequence element #0 has length 1; 2 is required
答案 0 :(得分:0)
在findCurrencyValue
{}创建d = dict(l)
,但l
看起来像
[
[
[
['Argentine Peso', ['9.44195', '0.10591']],
['Australian Dollar', ['1.41824', '0.70510']],
['Bahraini Dinar', ['0.37743', '2.64953']],
# etc
['Lithuanian Litas', ['3.07941', '0.32474']]
]
]
]
...即包含单个项目(列表)的列表。这不是dict()
的有效输入。请改为d = dict(records)
。
答案 1 :(得分:0)
HTML永远不应该被解析。以下是相同的示例,但请求 + lxml (行数较少且准确):
import requests
from lxml import html
URL = "https://www.cs.purdue.edu/homes/jind/exchangerate.html"
response = requests.get(URL)
tree = html.fromstring(response.content)
currency_dict = dict()
for row in tree.xpath('//table/tr')[1:]:
currency, oneUSD, oneUnit = row.xpath('.//td/text()')
currency_dict[currency] = dict(oneUSD=float(oneUSD), oneUnit=float(oneUnit))
search = "Argentine Peso" ## Change this value to the one you want to search for
oneUnit = currency_dict[search]['oneUnit']
oneUSD = currency_dict[search]['oneUSD']
print "Currency Exchange Rate for: {}".format(search)
print "1 USD = * Unit : {}".format(oneUSD)
print "1 Unit = * USD : {}".format(oneUnit)
输出:
Currency Exchange Rate for: Argentine Peso
1 USD = * Unit : 9.44195
1 Unit = * USD : 0.10591
替代 lxml (http://lxml.de/index.html#documentation) BeautifulSoup (http://www.crummy.com/software/BeautifulSoup/bs4/doc/)