如何使用Python(或R或其他编程语言)从网站上通过网络抓取练习英语考试题?

问题描述 投票:-2回答:1

我是网络抓取的新手,我需要一些有关网站(https://www.examenglish.com/KET/KET_grammar.htm)的网络抓取练习英语考试问题的帮助。

[该网站包含练习题,为各种英语考试做准备,而我想做的是提取网站中间显示的练习题中使用的文字。共有50个练习题,但50个问题未直接在网站上列出。相反,应该让用户选择当前问题的答案,然后单击出现在交互式屏幕底部的“下一步”按钮,以访问下一个问题。

尽管没有在网站上直接列出,但有没有一种方法可以使用python(或R,或者可以是其他编程语言)从所有50个练习题中提取文本?

我选择发布此问题,因为我根本不知道应该如何开始!

谢谢,

python r web text web-scraping
1个回答
0
投票

这是我在报废时使用的。请注意,您可以通过查看页面并查找要抓取的信息的/ div标签来找到所需的信息。 (只是一个示例站点,而不是您正在寻找的站点,但是会给您一个想法)

# Import libraries
import requests
import urllib.request
import time
from bs4 import BeautifulSoup

# Set the URL you want to webscrape from
url = 'http://web.mta.info/developers/turnstile.html'

# Connect to the URL
response = requests.get(url)

# Parse HTML and save to BeautifulSoup object¶
soup = BeautifulSoup(response.text, "html.parser")

# To download the whole data set, let's do a for loop through all a tags
for i in range(36,len(soup.findAll('a'))+1): #'a' tags are for links
    one_a_tag = soup.findAll('a')[i]
    link = one_a_tag['href']
    download_url = 'http://web.mta.info/developers/'+ link
    urllib.request.urlretrieve(download_url,'./'+link[link.find('/turnstile_')+1:]) 
    time.sleep(1) #pause the code for a sec

您必须安装Beautiful Soup才能使用此功能。

© www.soinside.com 2019 - 2024. All rights reserved.