Python replaces a torn multi-domain image URL

Source: Internet
Author: User

When the website is really a picture, often encounter the problem of incomplete picture link.

For example, the case is as follows:

The image link in the IMG tag is incomplete, if the site domain name is a variety of circumstances, such as

Http://sports.online.sh.cn/content/2018-03/13/content_8813151.htm

Http://sports.online.sh.cn/images/attachement/jpg/site1/20180313/IMG4ccc6a76b0f047094677984.JPG

Http://shenhua.online.sh.cn/content/2018-03/13/content_8813187.htm

Http://shenhua.online.sh.cn/images/attachement/jpg/site1/20180313/IMGd43d7e5f35354709509383.JPG

The two news is the same site, but different news pages, pictures of the link is incomplete, how to get a real picture link it?

First, we need to determine the domain name of the current page. Move your mouse over the picture's missing URL to see the full URL link. The missing part of the general picture link is the domain name part of the URL bar.

After that, we can make judgments in the code, such as:

    defParse_item (self, Response, spider): Self.item=Self.load_item (response)if 'Sports' inchresponse.url:self.item['content'] = self.item['content'].replace ('.. /.. /.. /images','http://sports.online.sh.cn/images')        elif 'Shenhua' inchresponse.url:self.item['content'] = self.item['content'].replace ('.. /.. /.. /images','http://shenhua.online.sh.cn/images')        yieldSelf.item

~ Using the member operator in to find the corresponding domain name, is a more practical and simple method of judging, the same can be used to judge the following several ways to achieve:

~ Use the index ()/rindex () method of the string module
The index ()/rindex () method is the same as the Find ()/rfind () method, except that a ValueError exception is reported when a substring is not found.

import stringdef find_string(s,t): try: string.index(s,t) return True except(ValueError): return Falses=‘nihao,shijie‘t=‘nihao‘result = find_string(s,t)print result #True

~ Use the Find ()/rfind (), index ()/rindex () and count () methods of the String object

>>> s=‘nihao,shijie‘>>> t=‘nihao‘>>> result = s.find(t)>=0>>> print resultTrue>>> result=s.count(t)>0>>> print resultTrue>>> result=s.index(t)>=0>>> print resultTrue

Python replaces a torn multi-domain picture URL

Related Article

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.