Home > other >  Help!!!!!! Regular expressions are correct, but the program doesn't run
Help!!!!!! Regular expressions are correct, but the program doesn't run

Time:09-26

 import requests 
The import json
The import re
The import time
The from requests. Exceptions import RequestException
Url='http://www.24timemap.com/'
Def get_one_page (url) :
Try:
Headers={
'the user-agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10 _13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.162 Safari/537.36 '
}
The response=requests. Get (url, headers=headers)
If the response. Status_code==200:
Return the response. The text
Return None
Except RequestException:
Return None
Def parse_one_page (HTML) :
The pattern=re.com running (' & lt; Li. *? Bg. *? The title. *?> (. *?) (. *?) 're. S)
The items=re. The.findall (pattern, HTML)
For the item in the items:
Yield {
'locantion: item [0],
'time: the item [1]
}

Def write_to_file (content) :
With the open, 'says Mr Galdi at SLW. TXT', 'a', encoding="utf-8") as f:
F.w rite (json. Dumps (content, ensure_ascii=False) + '\ n')

CodePudding user response:

There is something wrong with the indentation,
 
Def write_to_file (content) :
With the open, 'says Mr Galdi at SLW. TXT', 'a', encoding="utf-8") as f:
F.w rite (json. Dumps (content, ensure_ascii=False) + '\ n')



 

Def write_to_file (content) :
With the open, 'says Mr Galdi at SLW. TXT', 'a', encoding="utf-8") as f:
F.w rite (json. Dumps (content, ensure_ascii=False) + '\ n')


CodePudding user response:

Hello, I put the indentation in the format you give advance, but still can't run

CodePudding user response:

Code that defines the three functions, you want to do what?

CodePudding user response:

Regular expressions are correct, but the program doesn't run
Indeed, your code regular (perhaps) is right
And you say can't run, in fact, has been running for
- "already according to your Python code to run the
- "run the result: nothing dry
- "why didn't do, because you didn't write code to make it work
- specific said: "you only defines the three functions (which is a function of regular) as you say, but they didn't call (any one)
So into you said: "code cannot run
"
How to let it run?
Such as code then add

 
PageHtml=get_one_page (url)
RespGenerator=parse_one_page (pageHtml)
For eachItemDict respGenerator in:
Write_to_file (eachItemDict)


Can be achieved:
Really calling function performs the internal code and logic - "really run into said: you want code,


In addition, the indentation of code you code to run but I can't run, you thought see:
[tutorial], a Python code indentation (Indent) : the internal logic relationship affect code and results - in the way

  • Related