我正在尝试从网站获取表格数据。网页上显示了一些产品,我提取了这些产品的链接,并想将这些产品一一转到这些产品,获取表格,并将所有信息合并到一张大表格中。下面的代码仅返回 1 个产品的信息。有人可以帮助我将其扩展到所有产品吗?将是一个很大的帮助,谢谢。
import requests
import xlsxwriter
import csv
from bs4 import BeautifulSoup
def cpap_spider(max_pages):
global row_i
page=1
while page<=max_pages:
url= "https://dmesupplyusa.com/bath-aids.html?p=" +str(page)
source_code= requests.get(url)
plain_text= source_code.text
soup= BeautifulSoup(plain_text, 'html.parser')
for link in soup.findAll("a", {"class":"product-item-link"}):
href=link.get("href")
title = link.string
#worksheet.write(row_i, 0, title)
each_item(href)
print(href)
#print(title)
page+=1
def each_item(item_url):
source_code= requests.get(item_url)
plain_text= source_code.text
soup= BeautifulSoup(plain_text, 'html.parser')
table = soup.find("table", {"class":"data table additional-attributes"})
rows = table.find_all("tr")
with open("editors.csv", "wt+", newline="") as f:
writer = csv.writer(f)
for row in rows:
csv_row = [] …
Run Code Online (Sandbox Code Playgroud) 下面的代码取自https://github.com/anarn2/HierarchicalAttentionNetworks/blob/master/HierarchicalAttn.py,并进行了一些细微的调整。尽管我理解该错误的含义,但我无法弄清楚它是如何在以下代码中蔓延以及如何纠正它的。我已经被困在这个问题上很长一段时间了,非常感谢一些帮助。谢谢!
(这是整个代码)
maxlen = 100
max_sentences = 15
max_words = 20000
embedding_dim = 100
validation_split = 0.2
reviews = []
labels = []
texts = []
glove_dir = "./glove.6B"
embeddings_index = {}
# class defining the custom attention layer
class HierarchicalAttentionNetwork(Layer):
def __init__(self, attention_dim):
self.init = initializers.get('normal')
self.supports_masking = True
self.attention_dim = attention_dim
super(HierarchicalAttentionNetwork, self).__init__()
def build(self, input_shape):
assert len(input_shape) == 3
self.W = K.variable(self.init((input_shape[-1], self.attention_dim)))
self.b = K.variable(self.init((self.attention_dim,)))
self.u = K.variable(self.init((self.attention_dim, 1)))
self.trainable_weights = [self.W, self.b, self.u] …
Run Code Online (Sandbox Code Playgroud) 我有一个很大的数据集(大约 200k 行),我想将数据集随机分成两部分,70% 作为训练数据,30% 作为测试数据。有没有办法在 python 中做到这一点?注意我还想将这些数据集保存为我的计算机中的 Excel 或 csv 文件。谢谢!