我正在写一个刮板,它应该给我网站上显示的所有文本。如您在代码中所见,我有大约20个不同的网站需要抓取。我阅读了不同的站点和scrapy手册,并使用CrawlSpider构建了我的scraper(因为我认为这对于刮下子页面很有必要)。但是我不确定是否可以使用parse_item函数,因为我不使用项目。我想要网站的纯文本的原因是,我必须对这些文本进行内容分析。这是我到目前为止编写的代码:
import scrapy
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
from bs4 import BeautifulSoup
class MySpider(CrawlSpider):
name = 'eship4'
allowed_domains = [
'comodule.com',
'coolar.co',
'digimondo.de',
'emmy-sharing.de',
'shinepowered.com',
'envelio.de',
'allego.eu',
'terranova-energy.com',
'gridhound.de',
'solmove.com',
'bigchaindb',
'sunzilla.de',
'powerjames.com',
'graphdefined.com',
'osole.net',
'kennwert.org',
'ecoligo.com',
'adaptive-balancing.com',
'ineratec.de',
'smight.com',
]
start_urls = [
'http://www.comodule.com',
'http://coolar.co',
'http://www.digimondo.de',
'https://emmy-sharing.de/en/',
'https://www.shinepowered.com/en/',
'http://envelio.de/language/en/',
'https://www.allego.eu/?sl=eu',
'http://terranova-energy.com/en/',
'https://www.gridhound.de/en/',
'http://www.solmove.com/en/home-2-2/',
'https://www.bigchaindb.com/',
'https://sunzilla.de/',
'http://www.powerjames.com/',
'https://www.graphdefined.com/',
'http://osole.net/',
'http://kennwert.org/',
'https://ecoligo.com/',
'https://www.adaptive-balancing.com/',
'http://ineratec.de/?lang=en',
'https://smight.com/en/',
]
rules = [Rule(LinkExtractor(), callback='parse_item', follow=True)] # Follow any link scrapy finds (that is allowed).
def parse_item(self, response):
page = response.url.replace("/"," ").replace(":"," ")
filename = '/Users/peter/Desktop/crawling/%s.html' %page
body = response.body
soup = BeautifulSoup(body)
with open(filename, 'w') as f:
f.write(soup.get_text())
我得到输出,对于每个子页面,我都有一个html文件,这很好(也许一个文件包含网站的所有文本会更好)。但是在“输出”中,仍然显示很多空格,段落和函数(可能是Java脚本),如您在此处看到的:
img.wp-smiley,
img.emoji {
display: inline !important;
border: none !important;
box-shadow: none !important;
height: 1em !important;
width: 1em !important;
margin: 0 .07em !important;
vertical-align: -0.1em !important;
background: none !important;
padding: 0 !important;
}
.post-thumbnail img[src$='.svg'] { width: 100%; height: auto; }
作为所需的输出,我只希望网站和子页面上显示的文本。