python
  • python-requests
  • 2017-04-18 1 views 0 likes 
    0

    나는 다음 사이트 검색 파이썬 요청을 사용하고 있습니다 : 조건에 대한 https://www.investing.com/ "내구재 주문 미국을"파이썬 POST 요청

    나는 검사 패널의 "네트워크"탭에서 확인하고 보인다 그것은 단순히 'quotes_search_text': 다음과 같은 형식으로 수행 '내구재 주문 미국의

    그래서 내가 파이썬으로 시도 :

    URL = 'https://www.investing.com/' 
    data = {'quotes_search_text':'Durable Goods Orders US'} 
    resp = requests.post(URL, data=data, headers={ 'User-Agent': 'Mozilla/5.0', 'X-Requested-With': 'XMLHttpRequest'}) 
    

    그러나이 수동을하는 동안 내가 볼 수있는 결과를 반환 나던. 모든 검색 결과 (페이지 검사에 따라) 클래스 속성으로 "GS-제목을"이 있어야하지만 내가 할 때

    soup = BeautifulSoup(resp.text, 'html.parser') 
    soup.select(".gs-title") 
    

    내가 어떤 결과를 볼 수 없습니다 ... 는 POST 요청의 일부 측면이 있습니까 내가 고려하지 않은 것? (여기에 완전한 멍청한 녀석)

    +0

    'find_all' 셀렉터가 HTML 태그를 기대할 때 클래스 속성을 찾고 있다고 생각합니다. –

    +0

    @double_j 아니요. 클래스 속성을 찾으려고합니다. 대상 요소는 다음과 같습니다. 'United States Durable Goods Orders MoM' –

    +0

    괜찮습니다.하지만 BeautifulSoup은 지금 당장 가지고있는 방식대로 태그를 찾지 않을 것입니다. 'soup.find_all ('a', { 'class': 'gs-title'})' –

    답변

    1

    채팅에서 자세히 살펴본 후 많은 변화가 있습니다. 찾으려는 정보를 검색하려면 끝날 때 실행중인 JS를 실행해야합니다. 원하는 변수로 query 변수를 변경할 수 있습니다.

    import requests 
    import json 
    from urllib.parse import quote_plus 
    
    URL = 'https://www.googleapis.com/customsearch/v1element' 
    
    query = 'Durable Goods Orders US' 
    query_formatted = quote_plus(query) 
    
    data = { 
        'key':'AIzaSyCVAXiUzRYsML1Pv6RwSG1gunmMikTzQqY', 
        'num':10, 
        'hl':'en', 
        'prettyPrint':'true', 
        'source':'gcsc', 
        'gss':'.com', 
        'cx':'015447872197439536574:fy9sb1kxnp8', 
        'q':query_formatted, 
        'googlehost':'www.google.com' 
    } 
    headers = { 
        'User-Agent':'Mozilla/5.0', 
        'Referer':'https://www.investing.com/search?q=' + query_formatted, 
    } 
    resp = requests.get(URL, params=data, headers=headers) 
    
    j = json.loads(resp.text) 
    # print(resp.text) 
    for r in j['results']: 
        print(r['title'], r['url']) 
    
    관련 문제