Created
          May 9, 2020 16:07 
        
      - 
      
 - 
        
Save takidog/447e2231691617cbb14b3a515f63ff1e to your computer and use it in GitHub Desktop.  
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import requests | |
| from lxml import etree | |
| def get_session(): | |
| sess = requests.session() | |
| sess.headers.update( | |
| {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36"}) | |
| sess.verify = False | |
| return sess | |
| def main(session: requests.session): | |
| main_url = "https://www.hilife.com.tw/storeInquiry_street.aspx" | |
| main_page = session.get(main_url) | |
| if main_page.status_code != 200: | |
| return False | |
| post_data = get_form_value(html=main_page.text) | |
| post_data['__EVENTTARGET'] = "CITY" | |
| post_data['CITY'] = "基隆市" | |
| # Only change CITY so this value need taipei data. | |
| post_data['AREA'] = "信義區" | |
| req = session.post(url=main_url, data=post_data) | |
| if req.status_code != 200: | |
| return False | |
| post_data = get_form_value(html=req.text) | |
| post_data['__EVENTTARGET'] = "AREA" | |
| post_data['CITY'] = "基隆市" | |
| post_data['AREA'] = "仁愛區" | |
| req = session.post(url=main_url, data=post_data) | |
| def get_form_value(html: str): | |
| root = etree.HTML(html) | |
| hidden_input = root.xpath("//input[@type='hidden']") | |
| return {i.attrib['name']: i.attrib['value'] for i in hidden_input} | |
| if __name__ == "__main__": | |
| session = get_session() | |
| main(session=session) | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment