我刚开始使用BeautifulSoup,一开始就遇到了一个障碍。我查找了类似的帖子,但没有找到解决我的具体问题的方法,或者有一些基本的我不理解的东西。我的目标是从这个页面中提取日语单词和他们的英语翻译和例子。
https://iknow.jp/courses/566921
并将它们保存在dataFrame或csv文件中。
我可以看到解析后的输出和一些标记的内容,但是每当我尝试用我感兴趣的类请求一些东西时,我都得不到任何结果。首先,我想要得到一个日语单词的列表,我想我应该能够这样做:
import urllib
from bs4 import BeautifulSoup
url = ["https://iknow.jp/courses/566921"]
data = []
for pg in url:
r = urllib.request.urlopen(pg)
soup = BeautifulSoup(r,"html.parser")
soup.find_all("a", {"class": "cue"})
但是当我搜索response字段时,我什么也得不到:
responseList = soup.findAll('p', attrs={ "class" : "response"})
for word in responseList:
print(word)
我试着通过寻找孩子从树上往下移动,但无法到达我想要的文本。我将非常感谢你的帮助。以下是我试图提取的字段:
在jxpython的巨大帮助下,我现在遇到了一个新的挑战(也许这应该是一个新的线程,但它是非常相关的,所以这里可能没问题)。我的目标是创建一个dataframe或csv文件,每行包含一个日语单词、翻译和带有音译的示例。使用以下命令创建列表:
driver.find_elements_by_class_name()
driver.find_elements_by_xpath()
我得到了具有不同数量元素的列表,因此不可能轻松地创建数据帧。
# len(cues) 100
# len(responses) 100
# len(transliterations)279 stramge number because some words don't have transliterations
# len(texts) 200
# len(translations)200
音译列表包含单个单词和句子的混合音译。我认为为了能够获得内容来填充我的数据帧的第一行,我需要遍历
<li class="item">
内容(xpath?#/html/body/div2/div/div/section/div/section/div/div/ul/li1)和for each通过翻译、句子和音译提取单词...我不确定这是不是最好的方法……
例如,我希望在我的数据帧的第一行(从屏幕截图中突出显示的框中)获得的信息是:
行く,いく,去吧,日曜日は図書館に行きます、にちようびはとしょかんにいきます.,我星期天去图书馆。,私は夏休みにプールに行った。、わたしはなつやすみにプールにいった.在暑假期间,我去了游泳池。
发布于 2018-09-12 06:24:16
您试图抓取的标记不在源代码中。可能是因为页面是JavaScript呈现的。尝试此url以查看您自己:
视图-来源:https://iknow.jp/courses/566921
Python模块Selenium解决了这个问题。如果你愿意,我可以写一些代码给你开始。
下面是一些可以开始的代码:
from selenium import webdriver
url = 'https://iknow.jp/courses/566921'
driver = webdriver.Chrome()
driver.get(url)
driver.implicitly_wait(2)
cues = driver.find_elements_by_class_name('cue')
cues = [cue.text for cue in cues]
responses = driver.find_elements_by_class_name('response')
responses = [response.text for response in responses]
texts = driver.find_elements_by_xpath('//*[@class="sentence-text"]/p[1]')
texts = [text.text for text in texts]
transliterations = driver.find_elements_by_class_name('transliteration')
transliterations = [transliteration.text for transliteration in transliterations]
translations = driver.find_elements_by_class_name('translation')
translations = [translation.text for translation in translations]
driver.close()
注意:您首先需要安装webdriver。我选择chrome。这里有一个链接:https://chromedriver.storage.googleapis.com/index.html?path=2.41/。还要将此代码添加到您的路径中!如果您有任何其他问题,请让我知道!
https://stackoverflow.com/questions/52283213
复制相似问题