python将爬取的数据转为字典_python网页爬取时,将数据保存到字典是发生数组越界怎么解决...

数组越界解决不了,请各位指教,直接上代码,这是跟着视频敲的,但是我的就是有问题,在线等答案

import re

import requests

from bs4 import BeautifulSoup

import sys

def getHtmlPage(url):

try:

r=requests.get(url, timeout=10)

r.raise_for_status()

r.encoding=r.apparent_encoding

return r.text

except:

s = sys.exc_info()

print(("Error '%s' happened on line %d")% (s[1],s[2].tb_lineno))

def getID(id, url):

html=getHtmlPage(url)

soup=BeautifulSoup(html,"html.parser")

a=soup.find_all("a")

for i in a:

try:

# i.attrs 是字典类型,属性名是键,属性值是值,但是 attrs 使用时出错,直接用 get 获得属性值

h = i.get("href")

f = re.compile(r"[s][zh]\d{6}")

b = f.findall(h)

c = re.findall(r"[0-9]\d{5}", str(b)) # 得到 ['900923'] 前面的 sh 标识是哪个股市的

if b != []:

id.append(b)

except:

s = sys.exc_info()

print(s[1], s[2].tb_lineno)

print("lalalalallala")

def getInfo(id, htmlurl, fpath):

count = 1

for i in id:

url = htmlurl + "".join(i) + ".html" # 一定要将列表转换为字符串

html = getHtmlPage(url)

try:

infodict = {}

soup = BeautifulSoup(html, "html.parser")

stockInfo = soup.find("div", attrs={"class": "stock-bets"}) # 最外层 div

# 这里得到的是一个 tag 对象集所以要取第一个值

stockname = soup.find_all("a", attrs={"class": "bets-name"})[0]

infodict.update({"股票名称": stockname.text.split()[0]})#

keylist = stockInfo.find_all("dt")

valuelist = stockInfo.find_all("dd")

for i in range(len(valuelist)):

infodict[keylist[i].text] = valuelist[i].text # 存入字典,数组越界在这里!

with open(fpath, "a", encoding="utf-8") as f:

f.write(str(infodict) + '\n')

count = count + 1 # 只爬取20个页面的,太多了电脑遭不住

if count > 20:

break

except:

s = sys.exc_info()

print(s[1],s[2].tb_lineno)

continue

def main():

baiduURl = "https://gupiao.baidu.com/stock/"

dongfangURl = "http://quote.eastmoney.com/stocklist.html"

fpath = "F:/la.txt"

id = []

getID(id, dongfangURl)

getInfo(id, baiduURl, fpath)

print("lalal")

main()

错误提示信息bV2OlU?w=909&h=314

所得到的文本结果

bV2OlZ?w=1329&h=830

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用广度优先搜索(BFS)算法来解决二维数组的寻路问题。下面是一个简单的示例代码,用于找到从起点到终点的最短路径: ```python from collections import deque def bfs(matrix, start, end): # 获取二维数组的行数和列数 rows = len(matrix) cols = len(matrix[0]) # 定义四个方向的偏移量,上、下、左、右 directions = [(-1, 0), (1, 0), (0, -1), (0, 1)] # 使用队列来保存待访问的节点 queue = deque() queue.append(start) # 使用visited集合来记录已经访问过的节点 visited = set() visited.add(start) # 使用distances字典来记录每个节点到起点的距离 distances = {} distances[start] = 0 while queue: node = queue.popleft() if node == end: # 找到终点,返回最短路径长度 return distances[node] x, y = node for dx, dy in directions: new_x, new_y = x + dx, y + dy # 检查新的节点是否越界或已经访问过 if 0 <= new_x < rows and 0 <= new_y < cols and (new_x, new_y) not in visited and matrix[new_x][new_y] != 0: # 更新新节点的距离,并添加到队列和visited集合中 new_node = (new_x, new_y) queue.append(new_node) visited.add(new_node) distances[new_node] = distances[node] + 1 # 没有找到终点,返回-1表示无法到达 return -1 ``` 你可以根据实际情况调用这个函数,并传入相应的参数,例如起点坐标和终点坐标。注意,这个示例代码假设二维数组中的非零元素表示可以通过的路径,零表示障碍物。你可以根据实际需求进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值