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