我对爬行很陌生。我爬取了一个网页并提取了超链接,然后将其提供给 Apache Nutch 1.18。所有网址均因格式错误而被拒绝。我想要做的是爬取项目数据库页面,提取它们的超链接,然后分别爬取每个页面。
我使用 Scrapy 爬取了数据库页面并将结果保存为 Json 文件。然后我解析 json 文件以提取链接,并将这些链接提供给 Nutch 以对每个页面进行深度抓取。
我试图验证这些链接,但我知道它们都是错误的:
def url_check(url):
min_attr = ('scheme' , 'netloc')
try:
result = urlparse(url)
if all([result.scheme, result.netloc]):
print ('correct')
else:
print('wrong')
except:
print ('wrong')
我现在的目标是修复这些链接,以便 Nutch 接受它们。
这是我用来从 JSON 文件中提取链接的代码:
if __name__ == '__main__':
print('starting link extraction')
fname = "aifos.json"
with codecs.open(fname, "rb", encoding='utf-8') as f:
links_data = f.read()
json_data = simplejson.loads(links_data)
all_links =[]
for item in json_data:
website = item['link']
有人可以帮忙吗?我尝试了一些建议,但他们一直失败。
请注意,我不是要验证网址,我已经发现它们无效。我正在尝试修复它们。这些 URL 都有效。我已经访问过它们。我现在不确定我的原始抓取代码是否有问题。请看下面。“链接”对象是我现在遇到的问题。
def parse_dir_content(self, response):
items = AifosItem()
#all_projects = response.css('div.node__content')
title = response.css('span::text').extract()
country = response.css('.details__item::text').extract()
link = response.css('dd.details__item.details__item--long a::attr(href)').extract()
short_description = response.css('.field.field--name-field-short-description.field--type-text-long.field--label-hidden').extract()
long_description = response.css('.field.field--name-field-long-description.field--type-text-long.field--label-hidden').extract()
#long_description = response.css('.node__content--main').extract()
items['title'] = title
items['country'] = country
items['link'] = link
items['short_description'] = short_description
items['long_description'] = long_description
yield items
编辑:-这里的摘要是这样的-如何修复爬虫的格式错误的网址?单击这些 url 时确实有效,但爬虫将它们视为格式错误而拒绝它们,当我测试它们时,我得到它们无效的错误。我错过了解析吗?这就是我添加 Scrapy 爬取代码的原因,该代码用于从父页面中提取这些 url。