Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

用python实现bing的查询结果整合 #1

Open
shigen-fu opened this issue Aug 23, 2023 · 0 comments
Open

用python实现bing的查询结果整合 #1

shigen-fu opened this issue Aug 23, 2023 · 0 comments

Comments

@shigen-fu
Copy link

shigen-fu commented Aug 23, 2023

`
def scrape_bing_search_results(query, num_results=5):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36'
}
url = f"https://cn.bing.com/search?q={query}"

response = requests.get(url, headers=headers)
response.raise_for_status()

soup = BeautifulSoup(response.text, 'html.parser')
results = []
for i, result in enumerate(soup.select('.b_algo')[:num_results], 1):
    link_element = result.select_one('a')
    href = link_element['href']
    title = link_element.text

    abstract_element = result.select_one('.b_caption p')
    abstract = abstract_element.get_text() if abstract_element else ''

    results.append({'href': href, 'title': title, 'abstract': abstract})

return results

def main():
parser = argparse.ArgumentParser(description='Bing Search Results Scraper')
parser.add_argument('-q', '--query', help='The search query')
parser.add_argument('-c', '--count', type=int, default=5, help='Number of results to retrieve')
args = parser.parse_args()
if not args.query:
print(colored('query is empty', 'red'))
exit()
# 调用示例
search_results = scrape_bing_search_results(args.query, args.count)
for result in search_results:
print(colored(json.dumps(result, indent=2, ensure_ascii=False), 'yellow'))

if name == 'main':
main()
`

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant