鱼C论坛

 找回密码
 立即注册
查看: 2192|回复: 6

[技术交流] Python 成语接龙

[复制链接]
发表于 2020-3-30 19:45:07 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 qiuyouzhi 于 2020-3-30 19:54 编辑

Python 成语接龙


目标URL:https://chengyujielong.51240.com/

  1. from requests import get
  2. from bs4 import BeautifulSoup as BS

  3. def open_url(url):
  4.     headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36'}
  5.     res = get(url)
  6.     return res

  7. def find_chengyu(res, value):
  8.     soup = BS(res.text, "html.parser")
  9.     target = soup.find_all("a", target="_blank")
  10.     target2 = soup.find_all("div",style='text-align: center; clear: both; margin-top: 10px; margin-bottom: 20px;color: #F00;')
  11.     for each in target2:
  12.         if each.text == '对不起,您所输入的成语无法接龙。':
  13.             return False
  14.     for each in target:
  15.         yield each.text

  16. def main():
  17.     value = input("请输入成语:")
  18.     n = int(input("要打印多少成语:"))
  19.     url = 'https://chengyujielong.51240.com/%s__chengyujielong/' % value
  20.     r = open_url(url)
  21.     temp = find_chengyu(r, value)
  22.     if temp == False:
  23.         return
  24.     res = list(find_chengyu(r, value))[2:]
  25.     for i in range(n):
  26.         try:
  27.             if res[i] == "手机号码查询":
  28.                 break
  29.             print(res[i])
  30.         except:
  31.             print("找不到成语!")
  32.             break
  33.    

  34. if __name__ == "__main__":
  35.     main()
复制代码


刚才大佬 一个账号 也发了,因为我不爱用正则

所以各种判定方法都很牵强,有改进的地方

(最好是判定是否找到成语的)请指出!
   

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-3-30 19:52:06 | 显示全部楼层
沙发是我的了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-7 21:53:09 | 显示全部楼层
本帖最后由 乘号 于 2020-4-8 20:41 编辑


三楼归我
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-8 20:38:12 | 显示全部楼层

你这是三楼。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-8 20:41:39 | 显示全部楼层
一个账号 发表于 2020-4-8 20:38
你这是三楼。。。

。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-28 19:45:08 | 显示全部楼层
666
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-30 22:51

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表