>  Q&A  >  본문

python - scrapy에서 CrawlSpider를 사용하면 URL을 일치시킬 수 없습니다

내 크롤러 코드는 다음과 같습니다. 규칙을 얻지 못했습니다. 문제가 무엇인지 모르겠습니다.

으아악

실행 오류 알림:

으아악
为情所困为情所困2710일 전817

모든 응답(3)나는 대답할 것이다

  • 世界只因有你

    世界只因有你2017-05-18 10:53:02

    주로 allow_domains 문제입니다. 이렇게 코드를 작성하면 링크 캡처가 가능합니다. 으아아아 allow_domains的问题,你的提取规则是没问题的,代码这样写就能抓链接了

    # encoding: utf-8
    import time
    from tutorial.items import CrawlerItem
    from scrapy.spiders import CrawlSpider, Rule
    from scrapy.linkextractors import LinkExtractor
    
    
    class MoyanSpider(CrawlSpider):
        name = 'maoyan'
        allowed_domains = ["maoyan.com"]
        start_urls = ['http://maoyan.com/films']
    
        rules = (
            Rule(LinkExtractor(allow=(r"films/\d+.*")), callback='parse_item', follow=True),
        )
    
        def parse_item(self, response):
            print(response.url)
            item = CrawlerItem()
            try:
    
                time.sleep(2)
                item['name'] = response.text.find("p", class_="movie-brief-container").find("h3", class_="name").get_text()
                item['score'] = response.text.find("p", class_="movie-index-content score normal-score").find("span",
                                                                                                           class_="stonefont").get_text()
                url = "http://maoyan.com" + response.text.find("p", class_="channel-detail movie-item-title").find("a")["href"]
                item['id'] = response.url.split("/")[-1]
                temp = response.text.find("p", "movie-brief-container").find("ul").get_text()
                temp = temp.split('\n')
                item['tags'] = temp[1]
                item['countries'] = temp[3].strip()
                item['duration'] = temp[4].split('/')[-1]
                item['time'] = temp[6]
                return item
            except Exception as e:
                print(e)
    

    主要就是allow_domain别带上http://가장 중요한 것은 allow_domain을 사용하고 http:// 문자열을 가져오지 않는 것입니다.

    또한 구문 분석 모듈에 문제가 있습니다. 데이터가 있으면 직접 수정하실 수 있습니다.

    또한 이전 동창에 대해 불평하고 싶습니다. 그는 자신의 코드를 전혀 디버깅하지 않았으며 이렇게 대답했습니다.

    .

    회신하다
    0
  • 習慣沉默

    習慣沉默2017-05-18 10:53:02

    여러 모듈 구성 요소가 더 이상 사용되지 않으므로 유사한 모듈을 대신 사용할 수 있습니다

    회신하다
    0
  • 阿神

    阿神2017-05-18 10:53:02

    단순한 경고이며 오류는 없습니다. 크롤링한 웹사이트가 크롤링 방지 조치를 취하여 정상적으로 얻을 수 없게 되었을 수도 있습니다.

    회신하다
    0
  • 취소회신하다