抓取错误的网址

问题描述

我对爬行很陌生。我抓取了一个网页并提取了超链接,然后将其提供给 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?这些 url 在点击时确实有效,但爬虫将它们拒绝为格式错误,当我测试它们时,我收到它们无效的错误。我错过了解析吗?这就是为什么我添加了我的 Scrapy 抓取代码,用于从父页面提取这些 url。

解决方法

现在已经解决了这个问题。在此处找到了修复网址的方法:How can I prepend the 'http://' protocol to a url when necessary?

这修复了 Nutch 中的协议,但我也发现我需要在 nutch 中更新我的 regex-urlfilter.txt,因为我已经放入了一个正则表达式,使注入器拒绝不匹配的 url。有点尴尬,那个。