# 3.5 Yahoo奇摩字典

這個範例有一個要注意的地方, 就是header要記得帶入referer參數, 不然你看到的字典頁面會跟你從yahoo導過去的字典頁面有所差異.

```python
import requests
from bs4 import BeautifulSoup
import urllib.parse


YAHOO_DICTIONARY_URL = "https://tw.dictionary.yahoo.com/dictionary?p="
YAHOO_REFERER_VALUE = "https://tw.dictionary.yahoo.com/dictionary"


def get_web_content(url, query):
    query = urllib.parse.quote_plus(query)
    resp = requests.get(url + query, headers={'Referer': YAHOO_REFERER_VALUE})
    if resp.status_code != 200:
        print('Invalid url: ', resp.url)
        return None
    else:
        return resp.text


def get_dict_info(dom):
    soup = BeautifulSoup(dom, 'html5lib')
    for explain in soup.find('ul', 'explanations').find_all('li', 'exp-item'):
        print(explain.text)


def main():
    page = get_web_content(YAHOO_DICTIONARY_URL, 'Java')
    if page:
        get_dict_info(page)


if __name__ == '__main__':
    main()
```

輸出如下:

```
爪哇  
爪哇產的咖啡  
【電腦】在網際網路上的應用程式開發語言  

Process finished with exit code 0
```

原始碼[點我](https://github.com/yotsuba1022/web-crawler-practice/blob/master/ch3/yahoo_dictionary.py)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://clu.gitbook.io/python-web-crawler-note/35-yahooqi-mo-zi-dian.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
