Scrapy Crawl Cattle website Travel data

Source: Internet
Author: User

Description: The Scrapy frame has been used to crawl the tourist data of the cattle net, just begin to practicing, so only climbed four fields for testing, which is the name of the attraction, the location of the attraction, the opening time of the attraction, the description of the scenic spot, and the result of the crawl is JSON format.
Partial data:

Part of the code:

Problems encountered: Start_urls can not dynamically add a URL, this also needs to study, here is simply to crawl all the URLs are thrown into the start_urls inside, this is feasible, but the pretreatment of the Web site is time-consuming. Then there is the processing of encoding, in which the data from the beginning of the scrapy to JSON is always/uxxx type, which needs to be modified in pipeline.py, setting.py, as follows:
In pipelines.py, modify the code as follows:

def __init__ (self):
        self.file = Codecs.open (' Items.json ', ' WB ', encoding= ' Utf-8 ')
    #
    def process_item ( Self, item, spider): Line
        = Json.dumps (Dict (item), ensure_ascii=false) + "\ n"
        self.file.write (line)
        Return Item
    #
    def spider_closed (self, spider):
        self.file.close ()

In settings.py, add the following code:

Item_pipelines = {
    ' bdlv_spider.pipelines.BdlvSpiderPipeline ':

Where Bdlvspiderpipeline is the class name in pipelines.py.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.