python - 在单独的行中抓取问题和答案,例如 A1 列的第一行包含 que 1 第一行 A2 col catains answer for qes 1 等等
问题描述
在此处输入图片描述我想从以下网站https://www.sanfoundry.com/python-mcqs-basic-operators/抓取问卷
格式应使每个问题和答案都显示在不同的列中。如果问题只包含没有任何 html 标记的文本,那就更好了。
我使用scrapy来抓取这些信息,但我面临的问题是所有问题都出现在一行中,而所有答案都出现在另一行中。我需要显示为:问题 1 在一行中,该问题的相应答案在另一行中。
import scrapy
class QuestionSpider(scrapy.Spider):
name = "redbot"
start_urls = ['https://www.sanfoundry.com/python-mcqs-basic-operators/']
def parse(self, response):
#Extracting the content using xpath selectors
text = [
' '.join(
line.strip()
for line in response.css("div > p").extract_first()
if line.strip()
)
for p in response.xpath('//p')
]
votes = [
' '.join(
line.strip()
for line in response.css(".collapseomatic_content ::text") .extract_first()
if line.strip()
)
for p in response.xpath('//div')
]
#Give the extracted content row wise
for item in zip(text,votes):
#create a dictionary to store the scraped info
scraped_info ={
'texts' : item[0],
'vote' : item[1]
}
#yield or give the scraped info to scrapy
yield scraped_info
我需要使用scrapy代码在没有html标签的2个不同行中的每个问题和相应答案。请更正我提供的代码。
解决方案
推荐阅读
- java - ZGC 最大堆大小超过物理内存
- python - 如何将 3D 图像变成 2D 地图?
- java - Spring Batch - 从数据库读取 - 转换 - 并写入文件
- c++ - 使用 tbb::parallel_invoke 时导致分段错误错误的原因是什么?
- javascript - 为什么我不能用用户的 URL 填充我的数组?
- amazon-web-services - 如何将流量转移到 URL 中没有端口号的目标服务器
- python - Django 无法加载静态文件
- c# - ToString 转换截断 SQL CLR 中的 SqlString 值
- socrata - 运算符之间的 SODA 不适用于某些数据集
- python - 为什么这个 ListNode 的值在 Python 中不会改变?