马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 qiuyouzhi 于 2020-3-30 19:54 编辑
Python 成语接龙
目标URL:https://chengyujielong.51240.com/
from requests import get
from bs4 import BeautifulSoup as BS
def open_url(url):
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'}
res = get(url)
return res
def find_chengyu(res, value):
soup = BS(res.text, "html.parser")
target = soup.find_all("a", target="_blank")
target2 = soup.find_all("div",style='text-align: center; clear: both; margin-top: 10px; margin-bottom: 20px;color: #F00;')
for each in target2:
if each.text == '对不起,您所输入的成语无法接龙。':
return False
for each in target:
yield each.text
def main():
value = input("请输入成语:")
n = int(input("要打印多少成语:"))
url = 'https://chengyujielong.51240.com/%s__chengyujielong/' % value
r = open_url(url)
temp = find_chengyu(r, value)
if temp == False:
return
res = list(find_chengyu(r, value))[2:]
for i in range(n):
try:
if res[i] == "手机号码查询":
break
print(res[i])
except:
print("找不到成语!")
break
if __name__ == "__main__":
main()
刚才大佬 一个账号 也发了,因为我不爱用正则
所以各种判定方法都很牵强,有改进的地方
(最好是判定是否找到成语的)请指出!
|